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();
359 InitializeCriticalSection(&pAsyncRead->csFilter);
360 pAsyncRead->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": AsyncReader.csFilter");
362 pAsyncRead->pszFileName = NULL;
363 pAsyncRead->pmt = NULL;
365 *ppv = (LPVOID)pAsyncRead;
367 TRACE("-- created at %p\n", pAsyncRead);
372 /** IUnknown methods **/
374 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
376 AsyncReader *This = (AsyncReader *)iface;
378 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
382 if (IsEqualIID(riid, &IID_IUnknown))
384 else if (IsEqualIID(riid, &IID_IPersist))
386 else if (IsEqualIID(riid, &IID_IMediaFilter))
388 else if (IsEqualIID(riid, &IID_IBaseFilter))
390 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
391 *ppv = (LPVOID)(&This->lpVtblFSF);
395 IUnknown_AddRef((IUnknown *)(*ppv));
399 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking) && !IsEqualIID(riid, &IID_IVideoWindow))
400 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
402 return E_NOINTERFACE;
405 static ULONG WINAPI AsyncReader_AddRef(IBaseFilter * iface)
407 AsyncReader *This = (AsyncReader *)iface;
408 ULONG refCount = InterlockedIncrement(&This->refCount);
410 TRACE("(%p)->() AddRef from %d\n", This, refCount - 1);
415 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
417 AsyncReader *This = (AsyncReader *)iface;
418 ULONG refCount = InterlockedDecrement(&This->refCount);
420 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
424 if (This->pOutputPin)
427 if(SUCCEEDED(IPin_ConnectedTo(This->pOutputPin, &pConnectedTo)))
429 IPin_Disconnect(pConnectedTo);
430 IPin_Release(pConnectedTo);
432 IPin_Disconnect(This->pOutputPin);
433 IPin_Release(This->pOutputPin);
435 This->csFilter.DebugInfo->Spare[0] = 0;
436 DeleteCriticalSection(&This->csFilter);
438 CoTaskMemFree(This->pszFileName);
440 FreeMediaType(This->pmt);
448 /** IPersist methods **/
450 static HRESULT WINAPI AsyncReader_GetClassID(IBaseFilter * iface, CLSID * pClsid)
452 TRACE("(%p)\n", pClsid);
454 *pClsid = CLSID_AsyncReader;
459 /** IMediaFilter methods **/
461 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
463 AsyncReader *This = (AsyncReader *)iface;
467 This->state = State_Stopped;
472 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
474 AsyncReader *This = (AsyncReader *)iface;
478 This->state = State_Paused;
483 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
485 AsyncReader *This = (AsyncReader *)iface;
487 TRACE("(%x%08x)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
489 This->state = State_Running;
494 static HRESULT WINAPI AsyncReader_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
496 AsyncReader *This = (AsyncReader *)iface;
498 TRACE("(%u, %p)\n", dwMilliSecsTimeout, pState);
500 *pState = This->state;
505 static HRESULT WINAPI AsyncReader_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
507 /* AsyncReader *This = (AsyncReader *)iface;*/
509 TRACE("(%p)\n", pClock);
514 static HRESULT WINAPI AsyncReader_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
516 /* AsyncReader *This = (AsyncReader *)iface;*/
518 TRACE("(%p)\n", ppClock);
523 /** IBaseFilter methods **/
525 static HRESULT AsyncReader_GetPin(IBaseFilter *iface, ULONG pos, IPin **pin, DWORD *lastsynctick)
527 AsyncReader *This = (AsyncReader *)iface;
529 /* Our pins are almost static, not changing so setting static tick count is ok */
530 *lastsynctick = This->lastpinchange;
535 *pin = (IPin *)This->pOutputPin;
540 static HRESULT WINAPI AsyncReader_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
542 AsyncReader *This = (AsyncReader *)iface;
544 TRACE("(%p/%p)->(%p)\n", This, iface, ppEnum);
546 return IEnumPinsImpl_Construct(ppEnum, AsyncReader_GetPin, iface);
549 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
551 FIXME("(%s, %p)\n", debugstr_w(Id), ppPin);
556 static HRESULT WINAPI AsyncReader_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
558 AsyncReader *This = (AsyncReader *)iface;
560 TRACE("(%p)\n", pInfo);
562 strcpyW(pInfo->achName, This->filterInfo.achName);
563 pInfo->pGraph = This->filterInfo.pGraph;
566 IFilterGraph_AddRef(pInfo->pGraph);
571 static HRESULT WINAPI AsyncReader_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
573 AsyncReader *This = (AsyncReader *)iface;
575 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
578 strcpyW(This->filterInfo.achName, pName);
580 *This->filterInfo.achName = 0;
581 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
586 static HRESULT WINAPI AsyncReader_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
588 FIXME("(%p)\n", pVendorInfo);
593 static const IBaseFilterVtbl AsyncReader_Vtbl =
595 AsyncReader_QueryInterface,
598 AsyncReader_GetClassID,
602 AsyncReader_GetState,
603 AsyncReader_SetSyncSource,
604 AsyncReader_GetSyncSource,
605 AsyncReader_EnumPins,
607 AsyncReader_QueryFilterInfo,
608 AsyncReader_JoinFilterGraph,
609 AsyncReader_QueryVendorInfo
612 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
614 AsyncReader *This = impl_from_IFileSourceFilter(iface);
616 return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->lpVtbl, riid, ppv);
619 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
621 AsyncReader *This = impl_from_IFileSourceFilter(iface);
623 return IBaseFilter_AddRef((IFileSourceFilter*)&This->lpVtbl);
626 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
628 AsyncReader *This = impl_from_IFileSourceFilter(iface);
630 return IBaseFilter_Release((IFileSourceFilter*)&This->lpVtbl);
633 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
637 IAsyncReader * pReader = NULL;
638 AsyncReader *This = impl_from_IFileSourceFilter(iface);
640 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
643 /* FIXME: check the sharing values that native uses */
644 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
646 if (hFile == INVALID_HANDLE_VALUE)
648 return HRESULT_FROM_WIN32(GetLastError());
652 hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->lpVtbl, &This->csFilter, &This->pOutputPin);
653 This->lastpinchange = GetTickCount();
656 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
658 /* store file name & media type */
661 CoTaskMemFree(This->pszFileName);
663 FreeMediaType(This->pmt);
665 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
666 strcpyW(This->pszFileName, pszFileName);
668 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
671 This->pmt->bFixedSizeSamples = TRUE;
672 This->pmt->bTemporalCompression = FALSE;
673 This->pmt->cbFormat = 0;
674 This->pmt->pbFormat = NULL;
675 This->pmt->pUnk = NULL;
676 This->pmt->lSampleSize = 0;
677 This->pmt->formattype = FORMAT_None;
678 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype);
681 CoTaskMemFree(This->pmt);
686 CopyMediaType(This->pmt, pmt);
690 IAsyncReader_Release(pReader);
694 if (This->pOutputPin)
696 IPin_Release(This->pOutputPin);
697 This->pOutputPin = NULL;
700 CoTaskMemFree(This->pszFileName);
702 FreeMediaType(This->pmt);
703 This->pszFileName = NULL;
709 /* FIXME: check return codes */
713 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
715 AsyncReader *This = impl_from_IFileSourceFilter(iface);
717 TRACE("(%p, %p)\n", ppszFileName, pmt);
722 /* copy file name & media type if available, otherwise clear the outputs */
723 if (This->pszFileName)
725 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
726 strcpyW(*ppszFileName, This->pszFileName);
729 *ppszFileName = NULL;
734 CopyMediaType(pmt, This->pmt);
736 ZeroMemory(pmt, sizeof(*pmt));
742 static const IFileSourceFilterVtbl FileSource_Vtbl =
744 FileSource_QueryInterface,
748 FileSource_GetCurFile
752 /* the dwUserData passed back to user */
753 typedef struct DATAREQUEST
755 IMediaSample * pSample; /* sample passed to us by user */
756 DWORD_PTR dwUserData; /* user data passed to us */
757 OVERLAPPED ovl; /* our overlapped structure */
760 typedef struct FileAsyncReader
763 const struct IAsyncReaderVtbl * lpVtblAR;
767 /* Why would you need more? Every sample has its own handle */
770 CRITICAL_SECTION csList; /* critical section to prevent concurrency issues */
771 DATAREQUEST *sample_list;
773 /* Have a handle for every sample, and then one more as flushing handle */
777 static inline FileAsyncReader *impl_from_IAsyncReader( IAsyncReader *iface )
779 return (FileAsyncReader *)((char*)iface - FIELD_OFFSET(FileAsyncReader, lpVtblAR));
782 static HRESULT AcceptProcAFR(LPVOID iface, const AM_MEDIA_TYPE *pmt)
784 AsyncReader *This = (AsyncReader *)iface;
786 FIXME("(%p, %p)\n", iface, pmt);
788 if (IsEqualGUID(&pmt->majortype, &This->pmt->majortype) &&
789 IsEqualGUID(&pmt->subtype, &This->pmt->subtype) &&
790 IsEqualGUID(&pmt->formattype, &FORMAT_None))
796 /* overridden pin functions */
798 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
800 FileAsyncReader *This = (FileAsyncReader *)iface;
801 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
805 if (IsEqualIID(riid, &IID_IUnknown))
807 else if (IsEqualIID(riid, &IID_IPin))
809 else if (IsEqualIID(riid, &IID_IAsyncReader))
810 *ppv = (LPVOID)&This->lpVtblAR;
814 IUnknown_AddRef((IUnknown *)(*ppv));
818 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking))
819 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
821 return E_NOINTERFACE;
824 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
826 FileAsyncReader *This = (FileAsyncReader *)iface;
827 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
830 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
834 CoTaskMemFree(This->sample_list);
835 if (This->handle_list)
837 for (x = 0; x <= This->samples; ++x)
838 CloseHandle(This->handle_list[x]);
839 CoTaskMemFree(This->handle_list);
841 CloseHandle(This->hFile);
842 This->csList.DebugInfo->Spare[0] = 0;
843 DeleteCriticalSection(&This->csList);
850 static HRESULT WINAPI FileAsyncReaderPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
852 ENUMMEDIADETAILS emd;
853 FileAsyncReader *This = (FileAsyncReader *)iface;
855 TRACE("(%p)\n", ppEnum);
858 emd.pMediaTypes = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
860 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
863 static const IPinVtbl FileAsyncReaderPin_Vtbl =
865 FileAsyncReaderPin_QueryInterface,
867 FileAsyncReaderPin_Release,
869 OutputPin_ReceiveConnection,
871 IPinImpl_ConnectedTo,
872 IPinImpl_ConnectionMediaType,
873 IPinImpl_QueryPinInfo,
874 IPinImpl_QueryDirection,
876 IPinImpl_QueryAccept,
877 FileAsyncReaderPin_EnumMediaTypes,
878 IPinImpl_QueryInternalConnections,
879 OutputPin_EndOfStream,
880 OutputPin_BeginFlush,
885 /* Function called as a helper to IPin_Connect */
886 /* specific AM_MEDIA_TYPE - it cannot be NULL */
887 /* this differs from standard OutputPin_ConnectSpecific only in that it
888 * doesn't need the IMemInputPin interface on the receiving pin */
889 static HRESULT FileAsyncReaderPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
891 OutputPin *This = (OutputPin *)iface;
894 TRACE("(%p, %p)\n", pReceivePin, pmt);
895 dump_AM_MEDIA_TYPE(pmt);
897 /* FIXME: call queryacceptproc */
899 This->pin.pConnectedTo = pReceivePin;
900 IPin_AddRef(pReceivePin);
901 CopyMediaType(&This->pin.mtCurrent, pmt);
903 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
907 IPin_Release(This->pin.pConnectedTo);
908 This->pin.pConnectedTo = NULL;
909 FreeMediaType(&This->pin.mtCurrent);
912 TRACE(" -- %x\n", hr);
916 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
922 piOutput.dir = PINDIR_OUTPUT;
923 piOutput.pFilter = pBaseFilter;
924 strcpyW(piOutput.achName, wszOutputPinName);
925 hr = OutputPin_Construct(&FileAsyncReaderPin_Vtbl, sizeof(FileAsyncReader), &piOutput, NULL, pBaseFilter, AcceptProcAFR, pCritSec, ppPin);
929 FileAsyncReader *pPinImpl = (FileAsyncReader *)*ppPin;
930 pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
931 pPinImpl->hFile = hFile;
932 pPinImpl->bFlushing = FALSE;
933 pPinImpl->sample_list = NULL;
934 pPinImpl->handle_list = NULL;
935 pPinImpl->queued_number = 0;
936 pPinImpl->pin.pConnectSpecific = FileAsyncReaderPin_ConnectSpecific;
937 InitializeCriticalSection(&pPinImpl->csList);
938 pPinImpl->csList.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FileAsyncReader.csList");
945 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
947 FileAsyncReader *This = impl_from_IAsyncReader(iface);
949 return IPin_QueryInterface((IPin *)This, riid, ppv);
952 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
954 FileAsyncReader *This = impl_from_IAsyncReader(iface);
956 return IPin_AddRef((IPin *)This);
959 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
961 FileAsyncReader *This = impl_from_IAsyncReader(iface);
963 return IPin_Release((IPin *)This);
966 #define DEF_ALIGNMENT 1
968 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
970 FileAsyncReader *This = impl_from_IAsyncReader(iface);
974 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
976 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
977 pProps->cbAlign = DEF_ALIGNMENT;
981 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
982 /* FIXME: check we are still aligned */
985 IMemAllocator_AddRef(pPreferred);
986 *ppActual = pPreferred;
987 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
994 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
998 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
999 /* FIXME: check we are still aligned */
1002 *ppActual = pPreferred;
1003 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
1010 CoTaskMemFree(This->sample_list);
1011 if (This->handle_list)
1014 for (x = 0; x <= This->samples; ++x)
1015 CloseHandle(This->handle_list[x]);
1016 CoTaskMemFree(This->handle_list);
1019 This->samples = pProps->cBuffers;
1020 TRACE("Samples: %u\n", This->samples);
1021 This->sample_list = CoTaskMemAlloc(sizeof(This->sample_list[0]) * pProps->cBuffers);
1022 This->handle_list = CoTaskMemAlloc(sizeof(HANDLE) * (pProps->cBuffers + 1));
1024 if (This->sample_list && This->handle_list)
1027 ZeroMemory(This->sample_list, sizeof(This->sample_list[0]) * pProps->cBuffers);
1028 for (x = 0; x < This->samples; ++x)
1030 This->sample_list[x].ovl.hEvent = This->handle_list[x] = CreateEventW(NULL, 0, 0, NULL);
1032 This->handle_list[This->samples] = CreateEventW(NULL, 1, 0, NULL);
1033 This->pin.allocProps = *pProps;
1038 CoTaskMemFree(This->sample_list);
1039 CoTaskMemFree(This->handle_list);
1041 This->sample_list = NULL;
1042 This->handle_list = NULL;
1050 IMemAllocator_Release(pPreferred);
1053 TRACE("-- %x\n", hr);
1057 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
1058 * however, this would be quite complicated to do and may be a bit error prone */
1059 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
1062 REFERENCE_TIME Start;
1063 REFERENCE_TIME Stop;
1064 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1065 LPBYTE pBuffer = NULL;
1068 TRACE("(%p, %lx)\n", pSample, dwUser);
1073 /* get start and stop positions in bytes */
1075 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
1078 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1080 EnterCriticalSection(&This->csList);
1081 if (This->bFlushing)
1083 LeaveCriticalSection(&This->csList);
1084 return VFW_E_WRONG_STATE;
1087 wait = WaitForMultipleObjectsEx(This->samples, This->handle_list, FALSE, 0, FALSE);
1088 if (wait < This->samples - 1)
1089 SetEvent(This->handle_list[wait]);
1091 wait = This->samples;
1095 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
1096 DATAREQUEST *pDataRq;
1099 /* Try to insert above the waiting sample if possible */
1100 for (x = wait + 1; x < This->samples; ++x)
1102 if (!This->sample_list[x].pSample)
1106 if (x >= This->samples)
1107 for (x = 0; x < This->samples; ++x)
1109 if (!This->sample_list[x].pSample)
1113 assert(x < This->samples);
1114 InterlockedIncrement(&This->queued_number);
1116 pDataRq = This->sample_list + x;
1118 pDataRq->ovl.u.s.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
1119 pDataRq->ovl.u.s.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
1120 pDataRq->dwUserData = dwUser;
1122 /* we violate traditional COM rules here by maintaining
1123 * a reference to the sample, but not calling AddRef, but
1124 * that's what MSDN says to do */
1125 pDataRq->pSample = pSample;
1127 /* this is definitely not how it is implemented on Win9x
1128 * as they do not support async reads on files, but it is
1129 * sooo much easier to use this than messing around with threads!
1131 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1132 hr = HRESULT_FROM_WIN32(GetLastError());
1134 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1135 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1139 LeaveCriticalSection(&This->csList);
1141 TRACE("-- %x\n", hr);
1145 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1148 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1151 TRACE("(%u, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1156 if (!This->bFlushing)
1158 if (!This->queued_number)
1160 /* It could be that nothing is queued right now, but that can be fixed */
1161 WARN("Called without samples in queue and not flushing!!\n");
1164 /* wait for an object to read, or time out */
1165 buffer = WaitForMultipleObjectsEx(This->samples+1, This->handle_list, FALSE, dwTimeout, TRUE);
1167 if (buffer >= This->samples)
1169 if (buffer != This->samples)
1170 FIXME("Returned: %u (%08x)\n", buffer, GetLastError());
1174 else if (buffer < This->samples)
1175 InterlockedDecrement(&This->queued_number);
1178 EnterCriticalSection(&This->csList);
1179 if (This->bFlushing && buffer == ~0)
1181 for (buffer = 0; buffer < This->samples; ++buffer)
1183 if (This->sample_list[buffer].pSample)
1185 ResetEvent(This->handle_list[buffer]);
1189 if (buffer == This->samples)
1191 assert(!This->queued_number);
1196 InterlockedDecrement(&This->queued_number);
1203 REFERENCE_TIME rtStart, rtStop;
1204 REFERENCE_TIME rtSampleStart, rtSampleStop;
1205 DATAREQUEST *pDataRq = This->sample_list + buffer;
1208 /* get any errors */
1209 if (!This->bFlushing && !GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1210 hr = HRESULT_FROM_WIN32(GetLastError());
1212 /* Return the sample no matter what so it can be destroyed */
1213 *ppSample = pDataRq->pSample;
1214 *pdwUser = pDataRq->dwUserData;
1216 if (This->bFlushing)
1217 hr = VFW_E_WRONG_STATE;
1222 /* Set the time on the sample */
1223 IMediaSample_SetActualDataLength(pDataRq->pSample, dwBytes);
1225 rtStart = (DWORD64)pDataRq->ovl.u.s.Offset + ((DWORD64)pDataRq->ovl.u.s.OffsetHigh << 32);
1226 rtStart = MEDIATIME_FROM_BYTES(rtStart);
1227 rtStop = rtStart + MEDIATIME_FROM_BYTES(dwBytes);
1229 IMediaSample_GetTime(pDataRq->pSample, &rtSampleStart, &rtSampleStop);
1230 assert(rtStart == rtSampleStart);
1231 assert(rtStop <= rtSampleStop);
1233 IMediaSample_SetTime(pDataRq->pSample, &rtStart, &rtStop);
1234 assert(rtStart == rtSampleStart);
1236 assert(rtStop == rtSampleStop);
1238 assert(rtStop == rtStart);
1240 This->sample_list[buffer].pSample = NULL;
1242 LeaveCriticalSection(&This->csList);
1244 TRACE("-- %x\n", hr);
1248 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1250 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1253 REFERENCE_TIME tStart;
1254 REFERENCE_TIME tStop;
1257 TRACE("(%p)\n", pSample);
1259 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1262 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1265 hr = FileAsyncReader_SyncRead(iface,
1266 BYTES_FROM_MEDIATIME(tStart),
1267 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1270 TRACE("-- %x\n", hr);
1274 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1278 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1280 TRACE("(%x%08x, %d, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1282 ZeroMemory(&ovl, sizeof(ovl));
1284 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1285 /* NOTE: llPosition is the actual byte position to start reading from */
1286 ovl.u.s.Offset = (DWORD) llPosition;
1287 ovl.u.s.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1289 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1290 hr = HRESULT_FROM_WIN32(GetLastError());
1292 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1299 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1300 hr = HRESULT_FROM_WIN32(GetLastError());
1303 CloseHandle(ovl.hEvent);
1305 TRACE("-- %x\n", hr);
1309 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1313 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1315 TRACE("(%p, %p)\n", pTotal, pAvailable);
1317 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1318 (GetLastError() != NO_ERROR))
1319 return HRESULT_FROM_WIN32(GetLastError());
1321 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1323 *pAvailable = *pTotal;
1328 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1330 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1334 EnterCriticalSection(&This->csList);
1335 This->bFlushing = TRUE;
1336 CancelIo(This->hFile);
1337 SetEvent(This->handle_list[This->samples]);
1338 LeaveCriticalSection(&This->csList);
1343 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1345 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1350 EnterCriticalSection(&This->csList);
1351 ResetEvent(This->handle_list[This->samples]);
1352 This->bFlushing = FALSE;
1353 for (x = 0; x < This->samples; ++x)
1354 assert(!This->sample_list[x].pSample);
1356 LeaveCriticalSection(&This->csList);
1361 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1363 FileAsyncReader_QueryInterface,
1364 FileAsyncReader_AddRef,
1365 FileAsyncReader_Release,
1366 FileAsyncReader_RequestAllocator,
1367 FileAsyncReader_Request,
1368 FileAsyncReader_WaitForNext,
1369 FileAsyncReader_SyncReadAligned,
1370 FileAsyncReader_SyncRead,
1371 FileAsyncReader_Length,
1372 FileAsyncReader_BeginFlush,
1373 FileAsyncReader_EndFlush,