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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
24 #include "quartz_private.h"
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
38 static const WCHAR wszOutputPinName[] = { 'O','u','t','p','u','t',0 };
40 typedef struct AsyncReader
42 const IBaseFilterVtbl * lpVtbl;
43 const IFileSourceFilterVtbl * lpVtblFSF;
46 FILTER_INFO filterInfo;
48 CRITICAL_SECTION csFilter;
55 static const IBaseFilterVtbl AsyncReader_Vtbl;
56 static const IFileSourceFilterVtbl FileSource_Vtbl;
57 static const IAsyncReaderVtbl FileAsyncReader_Vtbl;
59 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
61 static inline AsyncReader *impl_from_IFileSourceFilter( IFileSourceFilter *iface )
63 return (AsyncReader *)((char*)iface - FIELD_OFFSET(AsyncReader, lpVtblFSF));
66 static WCHAR const mediatype_name[11] = {
67 'M', 'e', 'd', 'i', 'a', ' ', 'T', 'y', 'p', 'e', 0 };
68 static WCHAR const subtype_name[8] = {
69 'S', 'u', 'b', 't', 'y', 'p', 'e', 0 };
71 static HRESULT process_extensions(HKEY hkeyExtensions, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
82 /* Get the part of the name that matters */
83 extension = PathFindExtensionW(pszFileName);
84 if (*extension != '.')
87 l = RegOpenKeyExW(hkeyExtensions, extension, 0, KEY_READ, &hsub);
91 size = sizeof(keying);
92 l = RegQueryValueExW(hsub, mediatype_name, NULL, NULL, (LPBYTE)keying, &size);
94 CLSIDFromString(keying, majorType);
96 size = sizeof(keying);
98 l = RegQueryValueExW(hsub, subtype_name, NULL, NULL, (LPBYTE)keying, &size);
100 CLSIDFromString(keying, minorType);
109 static unsigned char byte_from_hex_char(WCHAR wHex)
111 switch (tolowerW(wHex))
123 return (wHex - '0') & 0xf;
130 return (wHex - 'a' + 10) & 0xf;
136 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
146 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
148 /* format: "offset, bytestocompare, mask, value" */
150 ulOffset = strtolW(wszPatternString, NULL, 10);
152 if (!(wszPatternString = strchrW(wszPatternString, ',')))
155 wszPatternString++; /* skip ',' */
157 ulBytes = strtolW(wszPatternString, NULL, 10);
159 pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
160 pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
161 pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
163 /* default mask is match everything */
164 memset(pbMask, 0xFF, ulBytes);
166 if (!(wszPatternString = strchrW(wszPatternString, ',')))
169 wszPatternString++; /* skip ',' */
173 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
176 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
178 if ((strpos % 2) == 1) /* odd numbered position */
179 pbMask[strpos / 2] |= byte_from_hex_char(*wszPatternString);
181 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
184 if (!(wszPatternString = strchrW(wszPatternString, ',')))
187 wszPatternString++; /* skip ',' */
192 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
195 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
197 if ((strpos % 2) == 1) /* odd numbered position */
198 pbValue[strpos / 2] |= byte_from_hex_char(*wszPatternString);
200 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
205 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
210 for (i = 0; i < ulBytes; i++)
211 if ((pbFile[i] & pbMask[i]) != pbValue[i])
218 HeapFree(GetProcessHeap(), 0, pbMask);
219 HeapFree(GetProcessHeap(), 0, pbValue);
220 HeapFree(GetProcessHeap(), 0, pbFile);
222 /* if we encountered no errors with this string, and there is a following tuple, then we
223 * have to match that as well to succeed */
224 if ((hr == S_OK) && (wszPatternString = strchrW(wszPatternString, ',')))
225 return process_pattern_string(wszPatternString + 1, pReader);
230 static HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
232 HKEY hkeyMediaType = NULL;
236 static const WCHAR wszMediaType[] = {'M','e','d','i','a',' ','T','y','p','e',0};
238 TRACE("(%p, %s, %p, %p)\n", pReader, debugstr_w(pszFileName), majorType, minorType);
240 CopyMemory(majorType, &GUID_NULL, sizeof(*majorType));
241 CopyMemory(minorType, &GUID_NULL, sizeof(*minorType));
243 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType);
244 hr = HRESULT_FROM_WIN32(lRet);
250 for (indexMajor = 0; !bFound; indexMajor++)
253 WCHAR wszMajorKeyName[CHARS_IN_GUID];
254 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
255 static const WCHAR wszExtensions[] = {'E','x','t','e','n','s','i','o','n','s',0};
257 if (RegEnumKeyExW(hkeyMediaType, indexMajor, wszMajorKeyName, &dwKeyNameLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
259 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
261 TRACE("%s\n", debugstr_w(wszMajorKeyName));
262 if (!strcmpW(wszExtensions, wszMajorKeyName))
264 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType) == S_OK)
271 for (indexMinor = 0; !bFound; indexMinor++)
274 WCHAR wszMinorKeyName[CHARS_IN_GUID];
275 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
279 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
282 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
285 TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
287 if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
290 for (indexValue = 0; !bFound; indexValue++)
293 WCHAR wszValueName[14]; /* longest name we should encounter will be "Source Filter" */
294 LPWSTR wszPatternString = HeapAlloc(GetProcessHeap(), 0, maxValueLen);
295 DWORD dwValueNameLen = sizeof(wszValueName) / sizeof(wszValueName[0]); /* remember this is in chars */
296 DWORD dwDataLen = maxValueLen; /* remember this is in bytes */
297 static const WCHAR wszSourceFilter[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
300 if ((temp = RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen)) != ERROR_SUCCESS)
302 HeapFree(GetProcessHeap(), 0, wszPatternString);
306 /* if it is not the source filter value */
307 if (strcmpW(wszValueName, wszSourceFilter))
309 if (process_pattern_string(wszPatternString, pReader) == S_OK)
311 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName, majorType)) &&
312 SUCCEEDED(CLSIDFromString(wszMinorKeyName, minorType)))
316 HeapFree(GetProcessHeap(), 0, wszPatternString);
318 CloseHandle(hkeyMinor);
321 CloseHandle(hkeyMajor);
324 CloseHandle(hkeyMediaType);
326 if (SUCCEEDED(hr) && !bFound)
328 ERR("Media class not found\n");
332 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType), qzdebugstr_guid(minorType));
337 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
339 AsyncReader *pAsyncRead;
342 return CLASS_E_NOAGGREGATION;
344 pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
347 return E_OUTOFMEMORY;
349 pAsyncRead->lpVtbl = &AsyncReader_Vtbl;
350 pAsyncRead->lpVtblFSF = &FileSource_Vtbl;
351 pAsyncRead->refCount = 1;
352 pAsyncRead->filterInfo.achName[0] = '\0';
353 pAsyncRead->filterInfo.pGraph = NULL;
354 pAsyncRead->pOutputPin = NULL;
356 InitializeCriticalSection(&pAsyncRead->csFilter);
357 pAsyncRead->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": AsyncReader.csFilter");
359 pAsyncRead->pszFileName = NULL;
360 pAsyncRead->pmt = NULL;
362 *ppv = (LPVOID)pAsyncRead;
364 TRACE("-- created at %p\n", pAsyncRead);
369 /** IUnknown methods **/
371 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
373 AsyncReader *This = (AsyncReader *)iface;
375 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
379 if (IsEqualIID(riid, &IID_IUnknown))
381 else if (IsEqualIID(riid, &IID_IPersist))
383 else if (IsEqualIID(riid, &IID_IMediaFilter))
385 else if (IsEqualIID(riid, &IID_IBaseFilter))
387 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
388 *ppv = (LPVOID)(&This->lpVtblFSF);
392 IUnknown_AddRef((IUnknown *)(*ppv));
396 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking))
397 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
399 return E_NOINTERFACE;
402 static ULONG WINAPI AsyncReader_AddRef(IBaseFilter * iface)
404 AsyncReader *This = (AsyncReader *)iface;
405 ULONG refCount = InterlockedIncrement(&This->refCount);
407 TRACE("(%p)->() AddRef from %d\n", This, refCount - 1);
412 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
414 AsyncReader *This = (AsyncReader *)iface;
415 ULONG refCount = InterlockedDecrement(&This->refCount);
417 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
421 if (This->pOutputPin)
424 if(SUCCEEDED(IPin_ConnectedTo(This->pOutputPin, &pConnectedTo)))
426 IPin_Disconnect(pConnectedTo);
427 IPin_Release(pConnectedTo);
429 IPin_Disconnect(This->pOutputPin);
430 IPin_Release(This->pOutputPin);
432 This->csFilter.DebugInfo->Spare[0] = 0;
433 DeleteCriticalSection(&This->csFilter);
442 /** IPersist methods **/
444 static HRESULT WINAPI AsyncReader_GetClassID(IBaseFilter * iface, CLSID * pClsid)
446 TRACE("(%p)\n", pClsid);
448 *pClsid = CLSID_AsyncReader;
453 /** IMediaFilter methods **/
455 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
457 AsyncReader *This = (AsyncReader *)iface;
461 This->state = State_Stopped;
466 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
468 AsyncReader *This = (AsyncReader *)iface;
472 This->state = State_Paused;
477 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
479 AsyncReader *This = (AsyncReader *)iface;
481 TRACE("(%x%08x)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
483 This->state = State_Running;
488 static HRESULT WINAPI AsyncReader_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
490 AsyncReader *This = (AsyncReader *)iface;
492 TRACE("(%u, %p)\n", dwMilliSecsTimeout, pState);
494 *pState = This->state;
499 static HRESULT WINAPI AsyncReader_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
501 /* AsyncReader *This = (AsyncReader *)iface;*/
503 TRACE("(%p)\n", pClock);
508 static HRESULT WINAPI AsyncReader_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
510 /* AsyncReader *This = (AsyncReader *)iface;*/
512 TRACE("(%p)\n", ppClock);
517 /** IBaseFilter methods **/
519 static HRESULT WINAPI AsyncReader_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
522 AsyncReader *This = (AsyncReader *)iface;
524 TRACE("(%p)\n", ppEnum);
526 epd.cPins = This->pOutputPin ? 1 : 0;
527 epd.ppPins = &This->pOutputPin;
528 return IEnumPinsImpl_Construct(&epd, ppEnum);
531 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
533 FIXME("(%s, %p)\n", debugstr_w(Id), ppPin);
538 static HRESULT WINAPI AsyncReader_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
540 AsyncReader *This = (AsyncReader *)iface;
542 TRACE("(%p)\n", pInfo);
544 strcpyW(pInfo->achName, This->filterInfo.achName);
545 pInfo->pGraph = This->filterInfo.pGraph;
548 IFilterGraph_AddRef(pInfo->pGraph);
553 static HRESULT WINAPI AsyncReader_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
555 AsyncReader *This = (AsyncReader *)iface;
557 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
560 strcpyW(This->filterInfo.achName, pName);
562 *This->filterInfo.achName = 0;
563 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
568 static HRESULT WINAPI AsyncReader_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
570 FIXME("(%p)\n", pVendorInfo);
575 static const IBaseFilterVtbl AsyncReader_Vtbl =
577 AsyncReader_QueryInterface,
580 AsyncReader_GetClassID,
584 AsyncReader_GetState,
585 AsyncReader_SetSyncSource,
586 AsyncReader_GetSyncSource,
587 AsyncReader_EnumPins,
589 AsyncReader_QueryFilterInfo,
590 AsyncReader_JoinFilterGraph,
591 AsyncReader_QueryVendorInfo
594 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
596 AsyncReader *This = impl_from_IFileSourceFilter(iface);
598 return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->lpVtbl, riid, ppv);
601 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
603 AsyncReader *This = impl_from_IFileSourceFilter(iface);
605 return IBaseFilter_AddRef((IFileSourceFilter*)&This->lpVtbl);
608 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
610 AsyncReader *This = impl_from_IFileSourceFilter(iface);
612 return IBaseFilter_Release((IFileSourceFilter*)&This->lpVtbl);
615 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
619 IAsyncReader * pReader = NULL;
620 AsyncReader *This = impl_from_IFileSourceFilter(iface);
622 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
625 /* FIXME: check the sharing values that native uses */
626 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
628 if (hFile == INVALID_HANDLE_VALUE)
630 return HRESULT_FROM_WIN32(GetLastError());
634 hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->lpVtbl, &This->csFilter, &This->pOutputPin);
637 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
639 /* store file name & media type */
642 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
643 strcpyW(This->pszFileName, pszFileName);
644 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
647 This->pmt->bFixedSizeSamples = TRUE;
648 This->pmt->bTemporalCompression = FALSE;
649 This->pmt->cbFormat = 0;
650 This->pmt->pbFormat = NULL;
651 This->pmt->pUnk = NULL;
652 This->pmt->lSampleSize = 0;
653 This->pmt->formattype = FORMAT_None;
654 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype);
657 CoTaskMemFree(This->pmt);
662 CopyMediaType(This->pmt, pmt);
666 IAsyncReader_Release(pReader);
670 if (This->pOutputPin)
672 IPin_Release(This->pOutputPin);
673 This->pOutputPin = NULL;
676 CoTaskMemFree(This->pszFileName);
677 This->pszFileName = NULL;
682 /* FIXME: check return codes */
686 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
688 AsyncReader *This = impl_from_IFileSourceFilter(iface);
690 TRACE("(%p, %p)\n", ppszFileName, pmt);
695 /* copy file name & media type if available, otherwise clear the outputs */
696 if (This->pszFileName)
698 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
699 strcpyW(*ppszFileName, This->pszFileName);
702 *ppszFileName = NULL;
707 CopyMediaType(pmt, This->pmt);
709 ZeroMemory(pmt, sizeof(*pmt));
715 static const IFileSourceFilterVtbl FileSource_Vtbl =
717 FileSource_QueryInterface,
721 FileSource_GetCurFile
725 /* the dwUserData passed back to user */
726 typedef struct DATAREQUEST
728 IMediaSample * pSample; /* sample passed to us by user */
729 DWORD_PTR dwUserData; /* user data passed to us */
730 OVERLAPPED ovl; /* our overlapped structure */
732 struct DATAREQUEST * pNext; /* next data request in list */
735 static void queue(DATAREQUEST * pHead, DATAREQUEST * pItem)
737 DATAREQUEST * pCurrent;
738 for (pCurrent = pHead; pCurrent->pNext; pCurrent = pCurrent->pNext)
740 pCurrent->pNext = pItem;
743 typedef struct FileAsyncReader
746 const struct IAsyncReaderVtbl * lpVtblAR;
751 DATAREQUEST * pHead; /* head of data request list */
752 CRITICAL_SECTION csList; /* critical section to protect operations on list */
755 static inline FileAsyncReader *impl_from_IAsyncReader( IAsyncReader *iface )
757 return (FileAsyncReader *)((char*)iface - FIELD_OFFSET(FileAsyncReader, lpVtblAR));
760 static HRESULT AcceptProcAFR(LPVOID iface, const AM_MEDIA_TYPE *pmt)
762 AsyncReader *This = (AsyncReader *)iface;
764 FIXME("(%p, %p)\n", iface, pmt);
766 if (IsEqualGUID(&pmt->majortype, &This->pmt->majortype) &&
767 IsEqualGUID(&pmt->subtype, &This->pmt->subtype) &&
768 IsEqualGUID(&pmt->formattype, &FORMAT_None))
774 /* overriden pin functions */
776 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
778 FileAsyncReader *This = (FileAsyncReader *)iface;
779 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
783 if (IsEqualIID(riid, &IID_IUnknown))
785 else if (IsEqualIID(riid, &IID_IPin))
787 else if (IsEqualIID(riid, &IID_IAsyncReader))
788 *ppv = (LPVOID)&This->lpVtblAR;
792 IUnknown_AddRef((IUnknown *)(*ppv));
796 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking))
797 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
799 return E_NOINTERFACE;
802 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
804 FileAsyncReader *This = (FileAsyncReader *)iface;
805 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
807 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
811 DATAREQUEST * pCurrent;
813 for (pCurrent = This->pHead; pCurrent; pCurrent = pNext)
815 pNext = pCurrent->pNext;
816 CoTaskMemFree(pCurrent);
818 CloseHandle(This->hFile);
819 CloseHandle(This->hEvent);
820 This->csList.DebugInfo->Spare[0] = 0;
821 DeleteCriticalSection(&This->csList);
828 static HRESULT WINAPI FileAsyncReaderPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
830 ENUMMEDIADETAILS emd;
831 FileAsyncReader *This = (FileAsyncReader *)iface;
833 TRACE("(%p)\n", ppEnum);
836 emd.pMediaTypes = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
838 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
841 static const IPinVtbl FileAsyncReaderPin_Vtbl =
843 FileAsyncReaderPin_QueryInterface,
845 FileAsyncReaderPin_Release,
847 OutputPin_ReceiveConnection,
849 IPinImpl_ConnectedTo,
850 IPinImpl_ConnectionMediaType,
851 IPinImpl_QueryPinInfo,
852 IPinImpl_QueryDirection,
854 IPinImpl_QueryAccept,
855 FileAsyncReaderPin_EnumMediaTypes,
856 IPinImpl_QueryInternalConnections,
857 OutputPin_EndOfStream,
858 OutputPin_BeginFlush,
863 /* Function called as a helper to IPin_Connect */
864 /* specific AM_MEDIA_TYPE - it cannot be NULL */
865 /* this differs from standard OutputPin_ConnectSpecific only in that it
866 * doesn't need the IMemInputPin interface on the receiving pin */
867 static HRESULT FileAsyncReaderPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
869 OutputPin *This = (OutputPin *)iface;
872 TRACE("(%p, %p)\n", pReceivePin, pmt);
873 dump_AM_MEDIA_TYPE(pmt);
875 /* FIXME: call queryacceptproc */
877 This->pin.pConnectedTo = pReceivePin;
878 IPin_AddRef(pReceivePin);
879 CopyMediaType(&This->pin.mtCurrent, pmt);
881 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
885 IPin_Release(This->pin.pConnectedTo);
886 This->pin.pConnectedTo = NULL;
887 FreeMediaType(&This->pin.mtCurrent);
890 TRACE(" -- %x\n", hr);
894 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
896 FileAsyncReader * pPinImpl;
901 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
904 return E_OUTOFMEMORY;
906 piOutput.dir = PINDIR_OUTPUT;
907 piOutput.pFilter = pBaseFilter;
908 strcpyW(piOutput.achName, wszOutputPinName);
910 if (SUCCEEDED(OutputPin_Init(&piOutput, NULL, pBaseFilter, AcceptProcAFR, pCritSec, &pPinImpl->pin)))
912 pPinImpl->pin.pin.lpVtbl = &FileAsyncReaderPin_Vtbl;
913 pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
914 pPinImpl->hFile = hFile;
915 pPinImpl->hEvent = CreateEventW(NULL, 0, 0, NULL);
916 pPinImpl->bFlushing = FALSE;
917 pPinImpl->pHead = NULL;
918 pPinImpl->pin.pConnectSpecific = FileAsyncReaderPin_ConnectSpecific;
919 InitializeCriticalSection(&pPinImpl->csList);
920 pPinImpl->csList.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FileAsyncReader.csList");
922 *ppPin = (IPin *)(&pPinImpl->pin.pin.lpVtbl);
926 CoTaskMemFree(pPinImpl);
932 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
934 FileAsyncReader *This = impl_from_IAsyncReader(iface);
936 return IPin_QueryInterface((IPin *)This, riid, ppv);
939 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
941 FileAsyncReader *This = impl_from_IAsyncReader(iface);
943 return IPin_AddRef((IPin *)This);
946 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
948 FileAsyncReader *This = impl_from_IAsyncReader(iface);
950 return IPin_Release((IPin *)This);
953 #define DEF_ALIGNMENT 1
955 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
959 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
961 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
962 pProps->cbAlign = DEF_ALIGNMENT;
966 ALLOCATOR_PROPERTIES PropsActual;
967 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
968 /* FIXME: check we are still aligned */
971 IMemAllocator_AddRef(pPreferred);
972 *ppActual = pPreferred;
973 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
980 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
984 ALLOCATOR_PROPERTIES PropsActual;
985 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
986 /* FIXME: check we are still aligned */
989 *ppActual = pPreferred;
990 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
999 IMemAllocator_Release(pPreferred);
1002 TRACE("-- %x\n", hr);
1006 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
1007 * however, this would be quite complicated to do and may be a bit error prone */
1008 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
1010 REFERENCE_TIME Start;
1011 REFERENCE_TIME Stop;
1012 DATAREQUEST * pDataRq;
1015 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1017 TRACE("(%p, %lx)\n", pSample, dwUser);
1019 /* check flushing state */
1020 if (This->bFlushing)
1021 return VFW_E_WRONG_STATE;
1023 if (!(pDataRq = CoTaskMemAlloc(sizeof(*pDataRq))))
1026 /* get start and stop positions in bytes */
1028 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
1031 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1035 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
1037 pDataRq->ovl.u.s.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
1038 pDataRq->ovl.u.s.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
1039 pDataRq->ovl.hEvent = This->hEvent;
1040 pDataRq->dwUserData = dwUser;
1041 pDataRq->pNext = NULL;
1042 /* we violate traditional COM rules here by maintaining
1043 * a reference to the sample, but not calling AddRef, but
1044 * that's what MSDN says to do */
1045 pDataRq->pSample = pSample;
1047 EnterCriticalSection(&This->csList);
1050 /* adds data request to end of list */
1051 queue(This->pHead, pDataRq);
1053 This->pHead = pDataRq;
1055 LeaveCriticalSection(&This->csList);
1057 /* this is definitely not how it is implemented on Win9x
1058 * as they do not support async reads on files, but it is
1059 * sooo much easier to use this than messing around with threads!
1061 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1062 hr = HRESULT_FROM_WIN32(GetLastError());
1064 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1065 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1069 if (FAILED(hr) && pDataRq)
1071 EnterCriticalSection(&This->csList);
1073 DATAREQUEST * pCurrent;
1074 for (pCurrent = This->pHead; pCurrent && pCurrent->pNext; pCurrent = pCurrent->pNext)
1075 if (pCurrent->pNext == pDataRq)
1077 pCurrent->pNext = pDataRq->pNext;
1081 LeaveCriticalSection(&This->csList);
1082 CoTaskMemFree(pDataRq);
1085 TRACE("-- %x\n", hr);
1089 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1093 DATAREQUEST * pDataRq = NULL;
1094 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1096 TRACE("(%u, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1098 /* FIXME: we could do with improving this by waiting for an array of event handles
1099 * and then determining which one finished and removing that from the list, otherwise
1100 * we will end up waiting for longer than we should do, if a later request finishes
1101 * before an earlier one */
1106 /* we return immediately if flushing */
1107 if (This->bFlushing)
1108 hr = VFW_E_WRONG_STATE;
1112 /* wait for the read to finish or timeout */
1113 if (WaitForSingleObject(This->hEvent, dwTimeout) == WAIT_TIMEOUT)
1118 EnterCriticalSection(&This->csList);
1120 pDataRq = This->pHead;
1121 if (pDataRq == NULL)
1124 This->pHead = pDataRq->pNext;
1126 LeaveCriticalSection(&This->csList);
1131 /* get any errors */
1132 if (!GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1133 hr = HRESULT_FROM_WIN32(GetLastError());
1138 IMediaSample_SetActualDataLength(pDataRq->pSample, dwBytes);
1139 *ppSample = pDataRq->pSample;
1140 *pdwUser = pDataRq->dwUserData;
1143 /* no need to close event handle since we will close it when the pin is destroyed */
1144 CoTaskMemFree(pDataRq);
1146 TRACE("-- %x\n", hr);
1150 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1152 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1155 REFERENCE_TIME tStart;
1156 REFERENCE_TIME tStop;
1159 TRACE("(%p)\n", pSample);
1161 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1164 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1167 hr = FileAsyncReader_SyncRead(iface,
1168 BYTES_FROM_MEDIATIME(tStart),
1169 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1172 TRACE("-- %x\n", hr);
1176 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1180 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1182 TRACE("(%x%08x, %d, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1184 ZeroMemory(&ovl, sizeof(ovl));
1186 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1187 /* NOTE: llPosition is the actual byte position to start reading from */
1188 ovl.u.s.Offset = (DWORD) llPosition;
1189 ovl.u.s.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1191 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1192 hr = HRESULT_FROM_WIN32(GetLastError());
1194 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1201 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1202 hr = HRESULT_FROM_WIN32(GetLastError());
1205 CloseHandle(ovl.hEvent);
1207 TRACE("-- %x\n", hr);
1211 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1215 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1217 TRACE("(%p, %p)\n", pTotal, pAvailable);
1219 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1220 (GetLastError() != NO_ERROR))
1221 return HRESULT_FROM_WIN32(GetLastError());
1223 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1225 *pAvailable = *pTotal;
1230 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1232 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1236 This->bFlushing = TRUE;
1237 CancelIo(This->hFile);
1238 SetEvent(This->hEvent);
1240 /* FIXME: free list */
1245 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1247 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1251 This->bFlushing = FALSE;
1256 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1258 FileAsyncReader_QueryInterface,
1259 FileAsyncReader_AddRef,
1260 FileAsyncReader_Release,
1261 FileAsyncReader_RequestAllocator,
1262 FileAsyncReader_Request,
1263 FileAsyncReader_WaitForNext,
1264 FileAsyncReader_SyncReadAligned,
1265 FileAsyncReader_SyncRead,
1266 FileAsyncReader_Length,
1267 FileAsyncReader_BeginFlush,
1268 FileAsyncReader_EndFlush,