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);
101 CLSIDFromString(keying, minorType);
110 static unsigned char byte_from_hex_char(WCHAR wHex)
112 switch (tolowerW(wHex))
124 return (wHex - '0') & 0xf;
131 return (wHex - 'a' + 10) & 0xf;
137 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
147 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
149 /* format: "offset, bytestocompare, mask, value" */
151 ulOffset = strtolW(wszPatternString, NULL, 10);
153 if (!(wszPatternString = strchrW(wszPatternString, ',')))
156 wszPatternString++; /* skip ',' */
158 ulBytes = strtolW(wszPatternString, NULL, 10);
160 pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
161 pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
162 pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
164 /* default mask is match everything */
165 memset(pbMask, 0xFF, ulBytes);
167 if (!(wszPatternString = strchrW(wszPatternString, ',')))
170 wszPatternString++; /* skip ',' */
174 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
177 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
179 if ((strpos % 2) == 1) /* odd numbered position */
180 pbMask[strpos / 2] |= byte_from_hex_char(*wszPatternString);
182 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
185 if (!(wszPatternString = strchrW(wszPatternString, ',')))
188 wszPatternString++; /* skip ',' */
193 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
196 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
198 if ((strpos % 2) == 1) /* odd numbered position */
199 pbValue[strpos / 2] |= byte_from_hex_char(*wszPatternString);
201 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
206 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
211 for (i = 0; i < ulBytes; i++)
212 if ((pbFile[i] & pbMask[i]) != pbValue[i])
219 HeapFree(GetProcessHeap(), 0, pbMask);
220 HeapFree(GetProcessHeap(), 0, pbValue);
221 HeapFree(GetProcessHeap(), 0, pbFile);
223 /* if we encountered no errors with this string, and there is a following tuple, then we
224 * have to match that as well to succeed */
225 if ((hr == S_OK) && (wszPatternString = strchrW(wszPatternString, ',')))
226 return process_pattern_string(wszPatternString + 1, pReader);
231 static HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
233 HKEY hkeyMediaType = NULL;
237 static const WCHAR wszMediaType[] = {'M','e','d','i','a',' ','T','y','p','e',0};
239 TRACE("(%p, %s, %p, %p)\n", pReader, debugstr_w(pszFileName), majorType, minorType);
241 *majorType = GUID_NULL;
242 *minorType = GUID_NULL;
244 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType);
245 hr = HRESULT_FROM_WIN32(lRet);
251 for (indexMajor = 0; !bFound; indexMajor++)
254 WCHAR wszMajorKeyName[CHARS_IN_GUID];
255 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
256 static const WCHAR wszExtensions[] = {'E','x','t','e','n','s','i','o','n','s',0};
258 if (RegEnumKeyExW(hkeyMediaType, indexMajor, wszMajorKeyName, &dwKeyNameLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
260 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
262 TRACE("%s\n", debugstr_w(wszMajorKeyName));
263 if (!strcmpW(wszExtensions, wszMajorKeyName))
265 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType) == S_OK)
272 for (indexMinor = 0; !bFound; indexMinor++)
275 WCHAR wszMinorKeyName[CHARS_IN_GUID];
276 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
280 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
283 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
286 TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
288 if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
291 for (indexValue = 0; !bFound; indexValue++)
294 WCHAR wszValueName[14]; /* longest name we should encounter will be "Source Filter" */
295 LPWSTR wszPatternString = HeapAlloc(GetProcessHeap(), 0, maxValueLen);
296 DWORD dwValueNameLen = sizeof(wszValueName) / sizeof(wszValueName[0]); /* remember this is in chars */
297 DWORD dwDataLen = maxValueLen; /* remember this is in bytes */
298 static const WCHAR wszSourceFilter[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
301 if ((temp = RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen)) != ERROR_SUCCESS)
303 HeapFree(GetProcessHeap(), 0, wszPatternString);
307 /* if it is not the source filter value */
308 if (strcmpW(wszValueName, wszSourceFilter))
310 if (process_pattern_string(wszPatternString, pReader) == S_OK)
312 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName, majorType)) &&
313 SUCCEEDED(CLSIDFromString(wszMinorKeyName, minorType)))
317 HeapFree(GetProcessHeap(), 0, wszPatternString);
319 CloseHandle(hkeyMinor);
322 CloseHandle(hkeyMajor);
325 CloseHandle(hkeyMediaType);
327 if (SUCCEEDED(hr) && !bFound)
329 ERR("Media class not found\n");
333 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType), qzdebugstr_guid(minorType));
338 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
340 AsyncReader *pAsyncRead;
343 return CLASS_E_NOAGGREGATION;
345 pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
348 return E_OUTOFMEMORY;
350 pAsyncRead->lpVtbl = &AsyncReader_Vtbl;
351 pAsyncRead->lpVtblFSF = &FileSource_Vtbl;
352 pAsyncRead->refCount = 1;
353 pAsyncRead->filterInfo.achName[0] = '\0';
354 pAsyncRead->filterInfo.pGraph = NULL;
355 pAsyncRead->pOutputPin = NULL;
357 InitializeCriticalSection(&pAsyncRead->csFilter);
358 pAsyncRead->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": AsyncReader.csFilter");
360 pAsyncRead->pszFileName = NULL;
361 pAsyncRead->pmt = NULL;
363 *ppv = (LPVOID)pAsyncRead;
365 TRACE("-- created at %p\n", pAsyncRead);
370 /** IUnknown methods **/
372 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
374 AsyncReader *This = (AsyncReader *)iface;
376 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
380 if (IsEqualIID(riid, &IID_IUnknown))
382 else if (IsEqualIID(riid, &IID_IPersist))
384 else if (IsEqualIID(riid, &IID_IMediaFilter))
386 else if (IsEqualIID(riid, &IID_IBaseFilter))
388 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
389 *ppv = (LPVOID)(&This->lpVtblFSF);
393 IUnknown_AddRef((IUnknown *)(*ppv));
397 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking) && !IsEqualIID(riid, &IID_IVideoWindow))
398 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
400 return E_NOINTERFACE;
403 static ULONG WINAPI AsyncReader_AddRef(IBaseFilter * iface)
405 AsyncReader *This = (AsyncReader *)iface;
406 ULONG refCount = InterlockedIncrement(&This->refCount);
408 TRACE("(%p)->() AddRef from %d\n", This, refCount - 1);
413 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
415 AsyncReader *This = (AsyncReader *)iface;
416 ULONG refCount = InterlockedDecrement(&This->refCount);
418 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
422 if (This->pOutputPin)
425 if(SUCCEEDED(IPin_ConnectedTo(This->pOutputPin, &pConnectedTo)))
427 IPin_Disconnect(pConnectedTo);
428 IPin_Release(pConnectedTo);
430 IPin_Disconnect(This->pOutputPin);
431 IPin_Release(This->pOutputPin);
433 This->csFilter.DebugInfo->Spare[0] = 0;
434 DeleteCriticalSection(&This->csFilter);
436 CoTaskMemFree(This->pszFileName);
438 FreeMediaType(This->pmt);
446 /** IPersist methods **/
448 static HRESULT WINAPI AsyncReader_GetClassID(IBaseFilter * iface, CLSID * pClsid)
450 TRACE("(%p)\n", pClsid);
452 *pClsid = CLSID_AsyncReader;
457 /** IMediaFilter methods **/
459 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
461 AsyncReader *This = (AsyncReader *)iface;
465 This->state = State_Stopped;
470 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
472 AsyncReader *This = (AsyncReader *)iface;
476 This->state = State_Paused;
481 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
483 AsyncReader *This = (AsyncReader *)iface;
485 TRACE("(%x%08x)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
487 This->state = State_Running;
492 static HRESULT WINAPI AsyncReader_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
494 AsyncReader *This = (AsyncReader *)iface;
496 TRACE("(%u, %p)\n", dwMilliSecsTimeout, pState);
498 *pState = This->state;
503 static HRESULT WINAPI AsyncReader_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
505 /* AsyncReader *This = (AsyncReader *)iface;*/
507 TRACE("(%p)\n", pClock);
512 static HRESULT WINAPI AsyncReader_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
514 /* AsyncReader *This = (AsyncReader *)iface;*/
516 TRACE("(%p)\n", ppClock);
521 /** IBaseFilter methods **/
523 static HRESULT WINAPI AsyncReader_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
526 AsyncReader *This = (AsyncReader *)iface;
528 TRACE("(%p)\n", ppEnum);
530 epd.cPins = This->pOutputPin ? 1 : 0;
531 epd.ppPins = &This->pOutputPin;
532 return IEnumPinsImpl_Construct(&epd, ppEnum);
535 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
537 FIXME("(%s, %p)\n", debugstr_w(Id), ppPin);
542 static HRESULT WINAPI AsyncReader_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
544 AsyncReader *This = (AsyncReader *)iface;
546 TRACE("(%p)\n", pInfo);
548 strcpyW(pInfo->achName, This->filterInfo.achName);
549 pInfo->pGraph = This->filterInfo.pGraph;
552 IFilterGraph_AddRef(pInfo->pGraph);
557 static HRESULT WINAPI AsyncReader_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
559 AsyncReader *This = (AsyncReader *)iface;
561 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
564 strcpyW(This->filterInfo.achName, pName);
566 *This->filterInfo.achName = 0;
567 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
572 static HRESULT WINAPI AsyncReader_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
574 FIXME("(%p)\n", pVendorInfo);
579 static const IBaseFilterVtbl AsyncReader_Vtbl =
581 AsyncReader_QueryInterface,
584 AsyncReader_GetClassID,
588 AsyncReader_GetState,
589 AsyncReader_SetSyncSource,
590 AsyncReader_GetSyncSource,
591 AsyncReader_EnumPins,
593 AsyncReader_QueryFilterInfo,
594 AsyncReader_JoinFilterGraph,
595 AsyncReader_QueryVendorInfo
598 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
600 AsyncReader *This = impl_from_IFileSourceFilter(iface);
602 return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->lpVtbl, riid, ppv);
605 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
607 AsyncReader *This = impl_from_IFileSourceFilter(iface);
609 return IBaseFilter_AddRef((IFileSourceFilter*)&This->lpVtbl);
612 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
614 AsyncReader *This = impl_from_IFileSourceFilter(iface);
616 return IBaseFilter_Release((IFileSourceFilter*)&This->lpVtbl);
619 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
623 IAsyncReader * pReader = NULL;
624 AsyncReader *This = impl_from_IFileSourceFilter(iface);
626 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
629 /* FIXME: check the sharing values that native uses */
630 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
632 if (hFile == INVALID_HANDLE_VALUE)
634 return HRESULT_FROM_WIN32(GetLastError());
638 hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->lpVtbl, &This->csFilter, &This->pOutputPin);
641 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
643 /* store file name & media type */
646 CoTaskMemFree(This->pszFileName);
648 FreeMediaType(This->pmt);
650 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
651 strcpyW(This->pszFileName, pszFileName);
653 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
656 This->pmt->bFixedSizeSamples = TRUE;
657 This->pmt->bTemporalCompression = FALSE;
658 This->pmt->cbFormat = 0;
659 This->pmt->pbFormat = NULL;
660 This->pmt->pUnk = NULL;
661 This->pmt->lSampleSize = 0;
662 This->pmt->formattype = FORMAT_None;
663 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype);
666 CoTaskMemFree(This->pmt);
671 CopyMediaType(This->pmt, pmt);
675 IAsyncReader_Release(pReader);
679 if (This->pOutputPin)
681 IPin_Release(This->pOutputPin);
682 This->pOutputPin = NULL;
685 CoTaskMemFree(This->pszFileName);
687 FreeMediaType(This->pmt);
688 This->pszFileName = NULL;
694 /* FIXME: check return codes */
698 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
700 AsyncReader *This = impl_from_IFileSourceFilter(iface);
702 TRACE("(%p, %p)\n", ppszFileName, pmt);
707 /* copy file name & media type if available, otherwise clear the outputs */
708 if (This->pszFileName)
710 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
711 strcpyW(*ppszFileName, This->pszFileName);
714 *ppszFileName = NULL;
719 CopyMediaType(pmt, This->pmt);
721 ZeroMemory(pmt, sizeof(*pmt));
727 static const IFileSourceFilterVtbl FileSource_Vtbl =
729 FileSource_QueryInterface,
733 FileSource_GetCurFile
737 /* the dwUserData passed back to user */
738 typedef struct DATAREQUEST
740 IMediaSample * pSample; /* sample passed to us by user */
741 DWORD_PTR dwUserData; /* user data passed to us */
742 OVERLAPPED ovl; /* our overlapped structure */
745 typedef struct FileAsyncReader
748 const struct IAsyncReaderVtbl * lpVtblAR;
752 /* Why would you need more? Every sample has its own handle */
755 CRITICAL_SECTION csList; /* critical section to prevent concurrency issues */
756 DATAREQUEST *sample_list;
760 static inline FileAsyncReader *impl_from_IAsyncReader( IAsyncReader *iface )
762 return (FileAsyncReader *)((char*)iface - FIELD_OFFSET(FileAsyncReader, lpVtblAR));
765 static HRESULT AcceptProcAFR(LPVOID iface, const AM_MEDIA_TYPE *pmt)
767 AsyncReader *This = (AsyncReader *)iface;
769 FIXME("(%p, %p)\n", iface, pmt);
771 if (IsEqualGUID(&pmt->majortype, &This->pmt->majortype) &&
772 IsEqualGUID(&pmt->subtype, &This->pmt->subtype) &&
773 IsEqualGUID(&pmt->formattype, &FORMAT_None))
779 /* overridden pin functions */
781 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
783 FileAsyncReader *This = (FileAsyncReader *)iface;
784 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
788 if (IsEqualIID(riid, &IID_IUnknown))
790 else if (IsEqualIID(riid, &IID_IPin))
792 else if (IsEqualIID(riid, &IID_IAsyncReader))
793 *ppv = (LPVOID)&This->lpVtblAR;
797 IUnknown_AddRef((IUnknown *)(*ppv));
801 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking))
802 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
804 return E_NOINTERFACE;
807 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
809 FileAsyncReader *This = (FileAsyncReader *)iface;
810 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
813 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
817 CoTaskMemFree(This->sample_list);
818 if (This->handle_list)
820 for (x = 0; x < This->samples; ++x)
821 CloseHandle(This->handle_list[x]);
822 CoTaskMemFree(This->handle_list);
824 CloseHandle(This->hFile);
825 This->csList.DebugInfo->Spare[0] = 0;
826 DeleteCriticalSection(&This->csList);
833 static HRESULT WINAPI FileAsyncReaderPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
835 ENUMMEDIADETAILS emd;
836 FileAsyncReader *This = (FileAsyncReader *)iface;
838 TRACE("(%p)\n", ppEnum);
841 emd.pMediaTypes = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
843 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
846 static const IPinVtbl FileAsyncReaderPin_Vtbl =
848 FileAsyncReaderPin_QueryInterface,
850 FileAsyncReaderPin_Release,
852 OutputPin_ReceiveConnection,
854 IPinImpl_ConnectedTo,
855 IPinImpl_ConnectionMediaType,
856 IPinImpl_QueryPinInfo,
857 IPinImpl_QueryDirection,
859 IPinImpl_QueryAccept,
860 FileAsyncReaderPin_EnumMediaTypes,
861 IPinImpl_QueryInternalConnections,
862 OutputPin_EndOfStream,
863 OutputPin_BeginFlush,
868 /* Function called as a helper to IPin_Connect */
869 /* specific AM_MEDIA_TYPE - it cannot be NULL */
870 /* this differs from standard OutputPin_ConnectSpecific only in that it
871 * doesn't need the IMemInputPin interface on the receiving pin */
872 static HRESULT FileAsyncReaderPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
874 OutputPin *This = (OutputPin *)iface;
877 TRACE("(%p, %p)\n", pReceivePin, pmt);
878 dump_AM_MEDIA_TYPE(pmt);
880 /* FIXME: call queryacceptproc */
882 This->pin.pConnectedTo = pReceivePin;
883 IPin_AddRef(pReceivePin);
884 CopyMediaType(&This->pin.mtCurrent, pmt);
886 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
890 IPin_Release(This->pin.pConnectedTo);
891 This->pin.pConnectedTo = NULL;
892 FreeMediaType(&This->pin.mtCurrent);
895 TRACE(" -- %x\n", hr);
899 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
905 piOutput.dir = PINDIR_OUTPUT;
906 piOutput.pFilter = pBaseFilter;
907 strcpyW(piOutput.achName, wszOutputPinName);
908 hr = OutputPin_Construct(&FileAsyncReaderPin_Vtbl, sizeof(FileAsyncReader), &piOutput, NULL, pBaseFilter, AcceptProcAFR, pCritSec, ppPin);
912 FileAsyncReader *pPinImpl = (FileAsyncReader *)*ppPin;
913 pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
914 pPinImpl->hFile = hFile;
915 pPinImpl->bFlushing = FALSE;
916 pPinImpl->sample_list = NULL;
917 pPinImpl->handle_list = NULL;
918 pPinImpl->queued_number = 0;
919 pPinImpl->pin.pConnectSpecific = FileAsyncReaderPin_ConnectSpecific;
920 InitializeCriticalSection(&pPinImpl->csList);
921 pPinImpl->csList.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FileAsyncReader.csList");
928 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
930 FileAsyncReader *This = impl_from_IAsyncReader(iface);
932 return IPin_QueryInterface((IPin *)This, riid, ppv);
935 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
937 FileAsyncReader *This = impl_from_IAsyncReader(iface);
939 return IPin_AddRef((IPin *)This);
942 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
944 FileAsyncReader *This = impl_from_IAsyncReader(iface);
946 return IPin_Release((IPin *)This);
949 #define DEF_ALIGNMENT 1
951 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
953 FileAsyncReader *This = impl_from_IAsyncReader(iface);
957 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
959 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
960 pProps->cbAlign = DEF_ALIGNMENT;
964 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
965 /* FIXME: check we are still aligned */
968 IMemAllocator_AddRef(pPreferred);
969 *ppActual = pPreferred;
970 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
977 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
981 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
982 /* FIXME: check we are still aligned */
985 *ppActual = pPreferred;
986 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
993 CoTaskMemFree(This->sample_list);
994 if (This->handle_list)
997 for (x = 0; x < This->samples; ++x)
998 CloseHandle(This->handle_list[x]);
999 CoTaskMemFree(This->handle_list);
1002 This->samples = pProps->cBuffers;
1003 TRACE("Samples: %u\n", This->samples);
1004 This->sample_list = CoTaskMemAlloc(sizeof(This->sample_list[0]) * pProps->cBuffers);
1005 This->handle_list = CoTaskMemAlloc(sizeof(HANDLE) * pProps->cBuffers);
1007 if (This->sample_list && This->handle_list)
1010 ZeroMemory(This->sample_list, sizeof(This->sample_list[0]) * pProps->cBuffers);
1011 for (x = 0; x < This->samples; ++x)
1013 This->sample_list[x].ovl.hEvent = This->handle_list[x] = CreateEventW(NULL, 0, 0, NULL);
1015 This->pin.allocProps = *pProps;
1020 CoTaskMemFree(This->sample_list);
1021 CoTaskMemFree(This->handle_list);
1023 This->sample_list = NULL;
1024 This->handle_list = NULL;
1032 IMemAllocator_Release(pPreferred);
1035 TRACE("-- %x\n", hr);
1039 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
1040 * however, this would be quite complicated to do and may be a bit error prone */
1041 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
1044 REFERENCE_TIME Start;
1045 REFERENCE_TIME Stop;
1046 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1047 LPBYTE pBuffer = NULL;
1049 TRACE("(%p, %lx)\n", pSample, dwUser);
1054 /* get start and stop positions in bytes */
1056 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
1059 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1061 EnterCriticalSection(&This->csList);
1062 if (This->bFlushing)
1064 LeaveCriticalSection(&This->csList);
1065 return VFW_E_WRONG_STATE;
1070 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
1071 DATAREQUEST *pDataRq;
1074 for (x = 0; x < This->samples; ++x)
1076 if (!This->sample_list[x].pSample)
1079 assert(x < This->samples);
1080 InterlockedIncrement(&This->queued_number);
1082 pDataRq = This->sample_list + x;
1084 pDataRq->ovl.u.s.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
1085 pDataRq->ovl.u.s.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
1086 pDataRq->dwUserData = dwUser;
1088 /* we violate traditional COM rules here by maintaining
1089 * a reference to the sample, but not calling AddRef, but
1090 * that's what MSDN says to do */
1091 pDataRq->pSample = pSample;
1093 /* this is definitely not how it is implemented on Win9x
1094 * as they do not support async reads on files, but it is
1095 * sooo much easier to use this than messing around with threads!
1097 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1098 hr = HRESULT_FROM_WIN32(GetLastError());
1100 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1101 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1105 LeaveCriticalSection(&This->csList);
1107 TRACE("-- %x\n", hr);
1111 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1114 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1117 TRACE("(%u, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1122 if (!This->bFlushing)
1124 if (!This->queued_number)
1126 ERR("Called without samples in queue and not flushing!!\n");
1130 /* wait for an object to read, or time out */
1131 buffer = WaitForMultipleObjectsEx(This->samples, This->handle_list, FALSE, dwTimeout, TRUE);
1133 if (buffer >= MAXIMUM_WAIT_OBJECTS)
1135 FIXME("Returned: %u (%08x)\n", buffer, GetLastError());
1140 InterlockedDecrement(&This->queued_number);
1143 EnterCriticalSection(&This->csList);
1144 if (This->bFlushing && buffer == ~0)
1146 for (buffer = 0; buffer < This->samples; ++buffer)
1148 if (This->sample_list[buffer].pSample)
1151 if (buffer == This->samples)
1153 assert(!This->queued_number);
1158 InterlockedDecrement(&This->queued_number);
1165 REFERENCE_TIME rtStart, rtStop;
1166 REFERENCE_TIME rtSampleStart, rtSampleStop;
1167 DATAREQUEST *pDataRq = This->sample_list + buffer;
1170 /* get any errors */
1171 if (!This->bFlushing && !GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1172 hr = HRESULT_FROM_WIN32(GetLastError());
1174 /* Return the sample no matter what so it can be destroyed */
1175 *ppSample = pDataRq->pSample;
1176 *pdwUser = pDataRq->dwUserData;
1178 if (This->bFlushing)
1179 hr = VFW_E_WRONG_STATE;
1184 /* Set the time on the sample */
1185 IMediaSample_SetActualDataLength(pDataRq->pSample, dwBytes);
1187 rtStart = (DWORD64)pDataRq->ovl.u.s.Offset + ((DWORD64)pDataRq->ovl.u.s.OffsetHigh << 32);
1188 rtStart = MEDIATIME_FROM_BYTES(rtStart);
1189 rtStop = rtStart + MEDIATIME_FROM_BYTES(dwBytes);
1191 IMediaSample_GetTime(pDataRq->pSample, &rtSampleStart, &rtSampleStop);
1192 assert(rtStart == rtSampleStart);
1193 assert(rtStop <= rtSampleStop);
1195 IMediaSample_SetTime(pDataRq->pSample, &rtStart, &rtStop);
1196 assert(rtStart == rtSampleStart);
1198 assert(rtStop == rtSampleStop);
1200 assert(rtStop == rtStart);
1202 This->sample_list[buffer].pSample = NULL;
1204 LeaveCriticalSection(&This->csList);
1206 TRACE("-- %x\n", hr);
1210 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1212 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1215 REFERENCE_TIME tStart;
1216 REFERENCE_TIME tStop;
1219 TRACE("(%p)\n", pSample);
1221 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1224 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1227 hr = FileAsyncReader_SyncRead(iface,
1228 BYTES_FROM_MEDIATIME(tStart),
1229 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1232 TRACE("-- %x\n", hr);
1236 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1240 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1242 TRACE("(%x%08x, %d, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1244 ZeroMemory(&ovl, sizeof(ovl));
1246 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1247 /* NOTE: llPosition is the actual byte position to start reading from */
1248 ovl.u.s.Offset = (DWORD) llPosition;
1249 ovl.u.s.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1251 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1252 hr = HRESULT_FROM_WIN32(GetLastError());
1254 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1261 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1262 hr = HRESULT_FROM_WIN32(GetLastError());
1265 CloseHandle(ovl.hEvent);
1267 TRACE("-- %x\n", hr);
1271 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1275 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1277 TRACE("(%p, %p)\n", pTotal, pAvailable);
1279 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1280 (GetLastError() != NO_ERROR))
1281 return HRESULT_FROM_WIN32(GetLastError());
1283 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1285 *pAvailable = *pTotal;
1290 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1292 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1296 EnterCriticalSection(&This->csList);
1297 This->bFlushing = TRUE;
1298 CancelIo(This->hFile);
1299 LeaveCriticalSection(&This->csList);
1304 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1306 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1310 EnterCriticalSection(&This->csList);
1311 This->bFlushing = FALSE;
1312 LeaveCriticalSection(&This->csList);
1317 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1319 FileAsyncReader_QueryInterface,
1320 FileAsyncReader_AddRef,
1321 FileAsyncReader_Release,
1322 FileAsyncReader_RequestAllocator,
1323 FileAsyncReader_Request,
1324 FileAsyncReader_WaitForNext,
1325 FileAsyncReader_SyncReadAligned,
1326 FileAsyncReader_SyncRead,
1327 FileAsyncReader_Length,
1328 FileAsyncReader_BeginFlush,
1329 FileAsyncReader_EndFlush,