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;
56 static const IBaseFilterVtbl AsyncReader_Vtbl;
57 static const IFileSourceFilterVtbl FileSource_Vtbl;
58 static const IAsyncReaderVtbl FileAsyncReader_Vtbl;
60 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
62 static inline AsyncReader *impl_from_IFileSourceFilter( IFileSourceFilter *iface )
64 return (AsyncReader *)((char*)iface - FIELD_OFFSET(AsyncReader, lpVtblFSF));
67 static WCHAR const mediatype_name[11] = {
68 'M', 'e', 'd', 'i', 'a', ' ', 'T', 'y', 'p', 'e', 0 };
69 static WCHAR const subtype_name[8] = {
70 'S', 'u', 'b', 't', 'y', 'p', 'e', 0 };
72 static HRESULT process_extensions(HKEY hkeyExtensions, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
83 /* Get the part of the name that matters */
84 extension = PathFindExtensionW(pszFileName);
85 if (*extension != '.')
88 l = RegOpenKeyExW(hkeyExtensions, extension, 0, KEY_READ, &hsub);
92 size = sizeof(keying);
93 l = RegQueryValueExW(hsub, mediatype_name, NULL, NULL, (LPBYTE)keying, &size);
95 CLSIDFromString(keying, majorType);
97 size = sizeof(keying);
99 l = RegQueryValueExW(hsub, subtype_name, NULL, NULL, (LPBYTE)keying, &size);
102 CLSIDFromString(keying, minorType);
111 static unsigned char byte_from_hex_char(WCHAR wHex)
113 switch (tolowerW(wHex))
125 return (wHex - '0') & 0xf;
132 return (wHex - 'a' + 10) & 0xf;
138 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
148 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
150 /* format: "offset, bytestocompare, mask, value" */
152 ulOffset = strtolW(wszPatternString, NULL, 10);
154 if (!(wszPatternString = strchrW(wszPatternString, ',')))
157 wszPatternString++; /* skip ',' */
159 ulBytes = strtolW(wszPatternString, NULL, 10);
161 pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
162 pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
163 pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
165 /* default mask is match everything */
166 memset(pbMask, 0xFF, ulBytes);
168 if (!(wszPatternString = strchrW(wszPatternString, ',')))
171 wszPatternString++; /* skip ',' */
175 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
178 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
180 if ((strpos % 2) == 1) /* odd numbered position */
181 pbMask[strpos / 2] |= byte_from_hex_char(*wszPatternString);
183 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
186 if (!(wszPatternString = strchrW(wszPatternString, ',')))
189 wszPatternString++; /* skip ',' */
194 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
197 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
199 if ((strpos % 2) == 1) /* odd numbered position */
200 pbValue[strpos / 2] |= byte_from_hex_char(*wszPatternString);
202 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
207 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
212 for (i = 0; i < ulBytes; i++)
213 if ((pbFile[i] & pbMask[i]) != pbValue[i])
220 HeapFree(GetProcessHeap(), 0, pbMask);
221 HeapFree(GetProcessHeap(), 0, pbValue);
222 HeapFree(GetProcessHeap(), 0, pbFile);
224 /* if we encountered no errors with this string, and there is a following tuple, then we
225 * have to match that as well to succeed */
226 if ((hr == S_OK) && (wszPatternString = strchrW(wszPatternString, ',')))
227 return process_pattern_string(wszPatternString + 1, pReader);
232 static HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
234 HKEY hkeyMediaType = NULL;
238 static const WCHAR wszMediaType[] = {'M','e','d','i','a',' ','T','y','p','e',0};
240 TRACE("(%p, %s, %p, %p)\n", pReader, debugstr_w(pszFileName), majorType, minorType);
242 *majorType = GUID_NULL;
243 *minorType = GUID_NULL;
245 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType);
246 hr = HRESULT_FROM_WIN32(lRet);
252 for (indexMajor = 0; !bFound; indexMajor++)
255 WCHAR wszMajorKeyName[CHARS_IN_GUID];
256 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
257 static const WCHAR wszExtensions[] = {'E','x','t','e','n','s','i','o','n','s',0};
259 if (RegEnumKeyExW(hkeyMediaType, indexMajor, wszMajorKeyName, &dwKeyNameLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
261 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
263 TRACE("%s\n", debugstr_w(wszMajorKeyName));
264 if (!strcmpW(wszExtensions, wszMajorKeyName))
266 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType) == S_OK)
273 for (indexMinor = 0; !bFound; indexMinor++)
276 WCHAR wszMinorKeyName[CHARS_IN_GUID];
277 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
281 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
284 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
287 TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
289 if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
292 for (indexValue = 0; !bFound; indexValue++)
295 WCHAR wszValueName[14]; /* longest name we should encounter will be "Source Filter" */
296 LPWSTR wszPatternString = HeapAlloc(GetProcessHeap(), 0, maxValueLen);
297 DWORD dwValueNameLen = sizeof(wszValueName) / sizeof(wszValueName[0]); /* remember this is in chars */
298 DWORD dwDataLen = maxValueLen; /* remember this is in bytes */
299 static const WCHAR wszSourceFilter[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
302 if ((temp = RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen)) != ERROR_SUCCESS)
304 HeapFree(GetProcessHeap(), 0, wszPatternString);
308 /* if it is not the source filter value */
309 if (strcmpW(wszValueName, wszSourceFilter))
311 if (process_pattern_string(wszPatternString, pReader) == S_OK)
313 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName, majorType)) &&
314 SUCCEEDED(CLSIDFromString(wszMinorKeyName, minorType)))
318 HeapFree(GetProcessHeap(), 0, wszPatternString);
320 CloseHandle(hkeyMinor);
323 CloseHandle(hkeyMajor);
326 CloseHandle(hkeyMediaType);
328 if (SUCCEEDED(hr) && !bFound)
330 ERR("Media class not found\n");
334 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType), qzdebugstr_guid(minorType));
339 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
341 AsyncReader *pAsyncRead;
344 return CLASS_E_NOAGGREGATION;
346 pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
349 return E_OUTOFMEMORY;
351 pAsyncRead->lpVtbl = &AsyncReader_Vtbl;
352 pAsyncRead->lpVtblFSF = &FileSource_Vtbl;
353 pAsyncRead->refCount = 1;
354 pAsyncRead->filterInfo.achName[0] = '\0';
355 pAsyncRead->filterInfo.pGraph = NULL;
356 pAsyncRead->pOutputPin = NULL;
357 pAsyncRead->lastpinchange = GetTickCount();
358 pAsyncRead->state = State_Stopped;
360 InitializeCriticalSection(&pAsyncRead->csFilter);
361 pAsyncRead->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": AsyncReader.csFilter");
363 pAsyncRead->pszFileName = NULL;
364 pAsyncRead->pmt = NULL;
366 *ppv = (LPVOID)pAsyncRead;
368 TRACE("-- created at %p\n", pAsyncRead);
373 /** IUnknown methods **/
375 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
377 AsyncReader *This = (AsyncReader *)iface;
379 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
383 if (IsEqualIID(riid, &IID_IUnknown))
385 else if (IsEqualIID(riid, &IID_IPersist))
387 else if (IsEqualIID(riid, &IID_IMediaFilter))
389 else if (IsEqualIID(riid, &IID_IBaseFilter))
391 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
392 *ppv = (LPVOID)(&This->lpVtblFSF);
396 IUnknown_AddRef((IUnknown *)(*ppv));
400 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking) && !IsEqualIID(riid, &IID_IVideoWindow))
401 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
403 return E_NOINTERFACE;
406 static ULONG WINAPI AsyncReader_AddRef(IBaseFilter * iface)
408 AsyncReader *This = (AsyncReader *)iface;
409 ULONG refCount = InterlockedIncrement(&This->refCount);
411 TRACE("(%p)->() AddRef from %d\n", This, refCount - 1);
416 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
418 AsyncReader *This = (AsyncReader *)iface;
419 ULONG refCount = InterlockedDecrement(&This->refCount);
421 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
425 if (This->pOutputPin)
428 if(SUCCEEDED(IPin_ConnectedTo(This->pOutputPin, &pConnectedTo)))
430 IPin_Disconnect(pConnectedTo);
431 IPin_Release(pConnectedTo);
433 IPin_Disconnect(This->pOutputPin);
434 IPin_Release(This->pOutputPin);
436 This->csFilter.DebugInfo->Spare[0] = 0;
437 DeleteCriticalSection(&This->csFilter);
439 CoTaskMemFree(This->pszFileName);
441 FreeMediaType(This->pmt);
449 /** IPersist methods **/
451 static HRESULT WINAPI AsyncReader_GetClassID(IBaseFilter * iface, CLSID * pClsid)
453 TRACE("(%p)\n", pClsid);
455 *pClsid = CLSID_AsyncReader;
460 /** IMediaFilter methods **/
462 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
464 AsyncReader *This = (AsyncReader *)iface;
468 This->state = State_Stopped;
473 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
475 AsyncReader *This = (AsyncReader *)iface;
479 This->state = State_Paused;
484 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
486 AsyncReader *This = (AsyncReader *)iface;
488 TRACE("(%x%08x)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
490 This->state = State_Running;
495 static HRESULT WINAPI AsyncReader_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
497 AsyncReader *This = (AsyncReader *)iface;
499 TRACE("(%u, %p)\n", dwMilliSecsTimeout, pState);
501 *pState = This->state;
506 static HRESULT WINAPI AsyncReader_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
508 /* AsyncReader *This = (AsyncReader *)iface;*/
510 TRACE("(%p)\n", pClock);
515 static HRESULT WINAPI AsyncReader_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
517 /* AsyncReader *This = (AsyncReader *)iface;*/
519 TRACE("(%p)\n", ppClock);
524 /** IBaseFilter methods **/
526 static HRESULT AsyncReader_GetPin(IBaseFilter *iface, ULONG pos, IPin **pin, DWORD *lastsynctick)
528 AsyncReader *This = (AsyncReader *)iface;
530 /* Our pins are almost static, not changing so setting static tick count is ok */
531 *lastsynctick = This->lastpinchange;
533 if (pos >= 1 || !This->pOutputPin)
536 *pin = (IPin *)This->pOutputPin;
541 static HRESULT WINAPI AsyncReader_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
543 AsyncReader *This = (AsyncReader *)iface;
545 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
547 return IEnumPinsImpl_Construct(ppEnum, AsyncReader_GetPin, iface);
550 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
552 FIXME("(%s, %p)\n", debugstr_w(Id), ppPin);
557 static HRESULT WINAPI AsyncReader_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
559 AsyncReader *This = (AsyncReader *)iface;
561 TRACE("(%p)\n", pInfo);
563 strcpyW(pInfo->achName, This->filterInfo.achName);
564 pInfo->pGraph = This->filterInfo.pGraph;
567 IFilterGraph_AddRef(pInfo->pGraph);
572 static HRESULT WINAPI AsyncReader_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
574 AsyncReader *This = (AsyncReader *)iface;
576 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
579 strcpyW(This->filterInfo.achName, pName);
581 *This->filterInfo.achName = 0;
582 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
587 static HRESULT WINAPI AsyncReader_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
589 FIXME("(%p)\n", pVendorInfo);
594 static const IBaseFilterVtbl AsyncReader_Vtbl =
596 AsyncReader_QueryInterface,
599 AsyncReader_GetClassID,
603 AsyncReader_GetState,
604 AsyncReader_SetSyncSource,
605 AsyncReader_GetSyncSource,
606 AsyncReader_EnumPins,
608 AsyncReader_QueryFilterInfo,
609 AsyncReader_JoinFilterGraph,
610 AsyncReader_QueryVendorInfo
613 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
615 AsyncReader *This = impl_from_IFileSourceFilter(iface);
617 return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->lpVtbl, riid, ppv);
620 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
622 AsyncReader *This = impl_from_IFileSourceFilter(iface);
624 return IBaseFilter_AddRef((IFileSourceFilter*)&This->lpVtbl);
627 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
629 AsyncReader *This = impl_from_IFileSourceFilter(iface);
631 return IBaseFilter_Release((IFileSourceFilter*)&This->lpVtbl);
634 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
638 IAsyncReader * pReader = NULL;
639 AsyncReader *This = impl_from_IFileSourceFilter(iface);
641 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
644 /* FIXME: check the sharing values that native uses */
645 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
647 if (hFile == INVALID_HANDLE_VALUE)
649 return HRESULT_FROM_WIN32(GetLastError());
653 hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->lpVtbl, &This->csFilter, &This->pOutputPin);
654 This->lastpinchange = GetTickCount();
657 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
659 /* store file name & media type */
662 CoTaskMemFree(This->pszFileName);
664 FreeMediaType(This->pmt);
666 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
667 strcpyW(This->pszFileName, pszFileName);
669 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
672 This->pmt->bFixedSizeSamples = TRUE;
673 This->pmt->bTemporalCompression = FALSE;
674 This->pmt->cbFormat = 0;
675 This->pmt->pbFormat = NULL;
676 This->pmt->pUnk = NULL;
677 This->pmt->lSampleSize = 0;
678 This->pmt->formattype = FORMAT_None;
679 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype);
682 CoTaskMemFree(This->pmt);
687 CopyMediaType(This->pmt, pmt);
691 IAsyncReader_Release(pReader);
695 if (This->pOutputPin)
697 IPin_Release(This->pOutputPin);
698 This->pOutputPin = NULL;
701 CoTaskMemFree(This->pszFileName);
703 FreeMediaType(This->pmt);
704 This->pszFileName = NULL;
710 /* FIXME: check return codes */
714 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
716 AsyncReader *This = impl_from_IFileSourceFilter(iface);
718 TRACE("(%p, %p)\n", ppszFileName, pmt);
723 /* copy file name & media type if available, otherwise clear the outputs */
724 if (This->pszFileName)
726 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
727 strcpyW(*ppszFileName, This->pszFileName);
730 *ppszFileName = NULL;
735 CopyMediaType(pmt, This->pmt);
737 ZeroMemory(pmt, sizeof(*pmt));
743 static const IFileSourceFilterVtbl FileSource_Vtbl =
745 FileSource_QueryInterface,
749 FileSource_GetCurFile
753 /* the dwUserData passed back to user */
754 typedef struct DATAREQUEST
756 IMediaSample * pSample; /* sample passed to us by user */
757 DWORD_PTR dwUserData; /* user data passed to us */
758 OVERLAPPED ovl; /* our overlapped structure */
761 typedef struct FileAsyncReader
764 const struct IAsyncReaderVtbl * lpVtblAR;
768 /* Why would you need more? Every sample has its own handle */
772 CRITICAL_SECTION csList; /* critical section to prevent concurrency issues */
773 DATAREQUEST *sample_list;
775 /* Have a handle for every sample, and then one more as flushing handle */
779 static inline FileAsyncReader *impl_from_IAsyncReader( IAsyncReader *iface )
781 return (FileAsyncReader *)((char*)iface - FIELD_OFFSET(FileAsyncReader, lpVtblAR));
784 static HRESULT AcceptProcAFR(LPVOID iface, const AM_MEDIA_TYPE *pmt)
786 AsyncReader *This = (AsyncReader *)iface;
788 FIXME("(%p, %p)\n", iface, pmt);
790 if (IsEqualGUID(&pmt->majortype, &This->pmt->majortype) &&
791 IsEqualGUID(&pmt->subtype, &This->pmt->subtype) &&
792 IsEqualGUID(&pmt->formattype, &FORMAT_None))
798 /* overridden pin functions */
800 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
802 FileAsyncReader *This = (FileAsyncReader *)iface;
803 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
807 if (IsEqualIID(riid, &IID_IUnknown))
809 else if (IsEqualIID(riid, &IID_IPin))
811 else if (IsEqualIID(riid, &IID_IAsyncReader))
812 *ppv = (LPVOID)&This->lpVtblAR;
816 IUnknown_AddRef((IUnknown *)(*ppv));
820 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking))
821 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
823 return E_NOINTERFACE;
826 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
828 FileAsyncReader *This = (FileAsyncReader *)iface;
829 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
832 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
836 CoTaskMemFree(This->sample_list);
837 if (This->handle_list)
839 for (x = 0; x <= This->samples; ++x)
840 CloseHandle(This->handle_list[x]);
841 CoTaskMemFree(This->handle_list);
843 CloseHandle(This->hFile);
844 This->csList.DebugInfo->Spare[0] = 0;
845 DeleteCriticalSection(&This->csList);
852 static HRESULT WINAPI FileAsyncReaderPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
854 ENUMMEDIADETAILS emd;
855 FileAsyncReader *This = (FileAsyncReader *)iface;
857 TRACE("(%p)\n", ppEnum);
860 emd.pMediaTypes = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
862 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
865 static const IPinVtbl FileAsyncReaderPin_Vtbl =
867 FileAsyncReaderPin_QueryInterface,
869 FileAsyncReaderPin_Release,
871 OutputPin_ReceiveConnection,
873 IPinImpl_ConnectedTo,
874 IPinImpl_ConnectionMediaType,
875 IPinImpl_QueryPinInfo,
876 IPinImpl_QueryDirection,
878 IPinImpl_QueryAccept,
879 FileAsyncReaderPin_EnumMediaTypes,
880 IPinImpl_QueryInternalConnections,
881 OutputPin_EndOfStream,
882 OutputPin_BeginFlush,
887 /* Function called as a helper to IPin_Connect */
888 /* specific AM_MEDIA_TYPE - it cannot be NULL */
889 /* this differs from standard OutputPin_ConnectSpecific only in that it
890 * doesn't need the IMemInputPin interface on the receiving pin */
891 static HRESULT FileAsyncReaderPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
893 OutputPin *This = (OutputPin *)iface;
896 TRACE("(%p, %p)\n", pReceivePin, pmt);
897 dump_AM_MEDIA_TYPE(pmt);
899 /* FIXME: call queryacceptproc */
901 This->pin.pConnectedTo = pReceivePin;
902 IPin_AddRef(pReceivePin);
903 CopyMediaType(&This->pin.mtCurrent, pmt);
905 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
909 IPin_Release(This->pin.pConnectedTo);
910 This->pin.pConnectedTo = NULL;
911 FreeMediaType(&This->pin.mtCurrent);
914 TRACE(" -- %x\n", hr);
918 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
924 piOutput.dir = PINDIR_OUTPUT;
925 piOutput.pFilter = pBaseFilter;
926 strcpyW(piOutput.achName, wszOutputPinName);
927 hr = OutputPin_Construct(&FileAsyncReaderPin_Vtbl, sizeof(FileAsyncReader), &piOutput, NULL, pBaseFilter, AcceptProcAFR, pCritSec, ppPin);
931 FileAsyncReader *pPinImpl = (FileAsyncReader *)*ppPin;
932 pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
933 pPinImpl->hFile = hFile;
934 pPinImpl->bFlushing = FALSE;
935 pPinImpl->sample_list = NULL;
936 pPinImpl->handle_list = NULL;
937 pPinImpl->queued_number = 0;
938 pPinImpl->pin.pConnectSpecific = FileAsyncReaderPin_ConnectSpecific;
939 InitializeCriticalSection(&pPinImpl->csList);
940 pPinImpl->csList.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FileAsyncReader.csList");
947 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
949 FileAsyncReader *This = impl_from_IAsyncReader(iface);
951 return IPin_QueryInterface((IPin *)This, riid, ppv);
954 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
956 FileAsyncReader *This = impl_from_IAsyncReader(iface);
958 return IPin_AddRef((IPin *)This);
961 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
963 FileAsyncReader *This = impl_from_IAsyncReader(iface);
965 return IPin_Release((IPin *)This);
968 #define DEF_ALIGNMENT 1
970 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
972 FileAsyncReader *This = impl_from_IAsyncReader(iface);
976 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
978 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
979 pProps->cbAlign = DEF_ALIGNMENT;
983 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
984 /* FIXME: check we are still aligned */
987 IMemAllocator_AddRef(pPreferred);
988 *ppActual = pPreferred;
989 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
996 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
1000 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
1001 /* FIXME: check we are still aligned */
1004 *ppActual = pPreferred;
1005 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
1012 CoTaskMemFree(This->sample_list);
1013 if (This->handle_list)
1016 for (x = 0; x <= This->samples; ++x)
1017 CloseHandle(This->handle_list[x]);
1018 CoTaskMemFree(This->handle_list);
1021 This->samples = pProps->cBuffers;
1022 This->oldest_sample = 0;
1023 TRACE("Samples: %u\n", This->samples);
1024 This->sample_list = CoTaskMemAlloc(sizeof(This->sample_list[0]) * pProps->cBuffers);
1025 This->handle_list = CoTaskMemAlloc(sizeof(HANDLE) * pProps->cBuffers * 2);
1027 if (This->sample_list && This->handle_list)
1030 ZeroMemory(This->sample_list, sizeof(This->sample_list[0]) * pProps->cBuffers);
1031 for (x = 0; x < This->samples; ++x)
1033 This->sample_list[x].ovl.hEvent = This->handle_list[x] = CreateEventW(NULL, 0, 0, NULL);
1034 if (x + 1 < This->samples)
1035 This->handle_list[This->samples + 1 + x] = This->handle_list[x];
1037 This->handle_list[This->samples] = CreateEventW(NULL, 1, 0, NULL);
1038 This->pin.allocProps = *pProps;
1043 CoTaskMemFree(This->sample_list);
1044 CoTaskMemFree(This->handle_list);
1046 This->sample_list = NULL;
1047 This->handle_list = NULL;
1055 IMemAllocator_Release(pPreferred);
1058 TRACE("-- %x\n", hr);
1062 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
1063 * however, this would be quite complicated to do and may be a bit error prone */
1064 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
1067 REFERENCE_TIME Start;
1068 REFERENCE_TIME Stop;
1069 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1070 LPBYTE pBuffer = NULL;
1072 TRACE("(%p, %lx)\n", pSample, dwUser);
1077 /* get start and stop positions in bytes */
1079 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
1082 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1084 EnterCriticalSection(&This->csList);
1085 if (This->bFlushing)
1087 LeaveCriticalSection(&This->csList);
1088 return VFW_E_WRONG_STATE;
1093 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
1094 DATAREQUEST *pDataRq;
1097 /* Try to insert above the waiting sample if possible */
1098 for (x = This->oldest_sample; x < This->samples; ++x)
1100 if (!This->sample_list[x].pSample)
1104 if (x >= This->samples)
1105 for (x = 0; x < This->oldest_sample; ++x)
1107 if (!This->sample_list[x].pSample)
1111 /* There must be a sample we have found */
1112 assert(x < This->samples);
1113 ++This->queued_number;
1115 pDataRq = This->sample_list + x;
1117 pDataRq->ovl.u.s.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
1118 pDataRq->ovl.u.s.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
1119 pDataRq->dwUserData = dwUser;
1121 /* we violate traditional COM rules here by maintaining
1122 * a reference to the sample, but not calling AddRef, but
1123 * that's what MSDN says to do */
1124 pDataRq->pSample = pSample;
1126 /* this is definitely not how it is implemented on Win9x
1127 * as they do not support async reads on files, but it is
1128 * sooo much easier to use this than messing around with threads!
1130 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1131 hr = HRESULT_FROM_WIN32(GetLastError());
1133 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1134 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1138 LeaveCriticalSection(&This->csList);
1140 TRACE("-- %x\n", hr);
1144 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1147 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1150 TRACE("(%u, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1155 EnterCriticalSection(&This->csList);
1156 if (!This->bFlushing)
1158 LONG oldest = This->oldest_sample;
1160 if (!This->queued_number)
1162 /* It could be that nothing is queued right now, but that can be fixed */
1163 WARN("Called without samples in queue and not flushing!!\n");
1165 LeaveCriticalSection(&This->csList);
1167 /* wait for an object to read, or time out */
1168 buffer = WaitForMultipleObjectsEx(This->samples+1, This->handle_list + oldest, FALSE, dwTimeout, TRUE);
1170 EnterCriticalSection(&This->csList);
1171 if (buffer <= This->samples)
1173 /* Re-scale the buffer back to normal */
1176 /* Uh oh, we overshot the flusher handle, renormalize it back to 0..Samples-1 */
1177 if (buffer > This->samples)
1178 buffer -= This->samples + 1;
1179 assert(buffer <= This->samples);
1182 if (buffer >= This->samples)
1184 if (buffer != This->samples)
1185 FIXME("Returned: %u (%08x)\n", buffer, GetLastError());
1190 --This->queued_number;
1193 if (This->bFlushing && buffer == ~0)
1195 for (buffer = 0; buffer < This->samples; ++buffer)
1197 if (This->sample_list[buffer].pSample)
1199 ResetEvent(This->handle_list[buffer]);
1203 if (buffer == This->samples)
1205 assert(!This->queued_number);
1210 --This->queued_number;
1217 REFERENCE_TIME rtStart, rtStop;
1218 REFERENCE_TIME rtSampleStart, rtSampleStop;
1219 DATAREQUEST *pDataRq = This->sample_list + buffer;
1222 /* get any errors */
1223 if (!This->bFlushing && !GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1224 hr = HRESULT_FROM_WIN32(GetLastError());
1226 /* Return the sample no matter what so it can be destroyed */
1227 *ppSample = pDataRq->pSample;
1228 *pdwUser = pDataRq->dwUserData;
1230 if (This->bFlushing)
1231 hr = VFW_E_WRONG_STATE;
1236 /* Set the time on the sample */
1237 IMediaSample_SetActualDataLength(pDataRq->pSample, dwBytes);
1239 rtStart = (DWORD64)pDataRq->ovl.u.s.Offset + ((DWORD64)pDataRq->ovl.u.s.OffsetHigh << 32);
1240 rtStart = MEDIATIME_FROM_BYTES(rtStart);
1241 rtStop = rtStart + MEDIATIME_FROM_BYTES(dwBytes);
1243 IMediaSample_GetTime(pDataRq->pSample, &rtSampleStart, &rtSampleStop);
1244 assert(rtStart == rtSampleStart);
1245 assert(rtStop <= rtSampleStop);
1247 IMediaSample_SetTime(pDataRq->pSample, &rtStart, &rtStop);
1248 assert(rtStart == rtSampleStart);
1250 assert(rtStop == rtSampleStop);
1252 assert(rtStop == rtStart);
1254 This->sample_list[buffer].pSample = NULL;
1255 assert(This->oldest_sample < This->samples);
1257 if (buffer == This->oldest_sample)
1260 for (x = This->oldest_sample + 1; x < This->samples; ++x)
1261 if (This->sample_list[x].pSample)
1263 if (x >= This->samples)
1264 for (x = 0; x < This->oldest_sample; ++x)
1265 if (This->sample_list[x].pSample)
1267 if (This->oldest_sample == x)
1268 /* No samples found, reset to 0 */
1270 This->oldest_sample = x;
1273 LeaveCriticalSection(&This->csList);
1275 TRACE("-- %x\n", hr);
1279 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1281 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1284 REFERENCE_TIME tStart;
1285 REFERENCE_TIME tStop;
1288 TRACE("(%p)\n", pSample);
1290 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1293 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1296 hr = FileAsyncReader_SyncRead(iface,
1297 BYTES_FROM_MEDIATIME(tStart),
1298 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1301 TRACE("-- %x\n", hr);
1305 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1309 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1311 TRACE("(%x%08x, %d, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1313 ZeroMemory(&ovl, sizeof(ovl));
1315 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1316 /* NOTE: llPosition is the actual byte position to start reading from */
1317 ovl.u.s.Offset = (DWORD) llPosition;
1318 ovl.u.s.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1320 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1321 hr = HRESULT_FROM_WIN32(GetLastError());
1323 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1330 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1331 hr = HRESULT_FROM_WIN32(GetLastError());
1334 CloseHandle(ovl.hEvent);
1336 TRACE("-- %x\n", hr);
1340 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1344 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1346 TRACE("(%p, %p)\n", pTotal, pAvailable);
1348 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1349 (GetLastError() != NO_ERROR))
1350 return HRESULT_FROM_WIN32(GetLastError());
1352 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1354 *pAvailable = *pTotal;
1359 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1361 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1365 EnterCriticalSection(&This->csList);
1366 This->bFlushing = TRUE;
1367 CancelIo(This->hFile);
1368 SetEvent(This->handle_list[This->samples]);
1369 LeaveCriticalSection(&This->csList);
1374 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1376 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1381 EnterCriticalSection(&This->csList);
1382 ResetEvent(This->handle_list[This->samples]);
1383 This->bFlushing = FALSE;
1384 for (x = 0; x < This->samples; ++x)
1385 assert(!This->sample_list[x].pSample);
1387 LeaveCriticalSection(&This->csList);
1392 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1394 FileAsyncReader_QueryInterface,
1395 FileAsyncReader_AddRef,
1396 FileAsyncReader_Release,
1397 FileAsyncReader_RequestAllocator,
1398 FileAsyncReader_Request,
1399 FileAsyncReader_WaitForNext,
1400 FileAsyncReader_SyncReadAligned,
1401 FileAsyncReader_SyncRead,
1402 FileAsyncReader_Length,
1403 FileAsyncReader_BeginFlush,
1404 FileAsyncReader_EndFlush,