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
43 const IFileSourceFilterVtbl * lpVtblFSF;
50 static const IBaseFilterVtbl AsyncReader_Vtbl;
51 static const IFileSourceFilterVtbl FileSource_Vtbl;
52 static const IAsyncReaderVtbl FileAsyncReader_Vtbl;
54 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
56 static inline AsyncReader *impl_from_IFileSourceFilter( IFileSourceFilter *iface )
58 return (AsyncReader *)((char*)iface - FIELD_OFFSET(AsyncReader, lpVtblFSF));
61 static WCHAR const mediatype_name[11] = {
62 'M', 'e', 'd', 'i', 'a', ' ', 'T', 'y', 'p', 'e', 0 };
63 static WCHAR const subtype_name[8] = {
64 'S', 'u', 'b', 't', 'y', 'p', 'e', 0 };
66 static HRESULT process_extensions(HKEY hkeyExtensions, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
77 /* Get the part of the name that matters */
78 extension = PathFindExtensionW(pszFileName);
79 if (*extension != '.')
82 l = RegOpenKeyExW(hkeyExtensions, extension, 0, KEY_READ, &hsub);
86 size = sizeof(keying);
87 l = RegQueryValueExW(hsub, mediatype_name, NULL, NULL, (LPBYTE)keying, &size);
89 CLSIDFromString(keying, majorType);
91 size = sizeof(keying);
93 l = RegQueryValueExW(hsub, subtype_name, NULL, NULL, (LPBYTE)keying, &size);
96 CLSIDFromString(keying, minorType);
105 static unsigned char byte_from_hex_char(WCHAR wHex)
107 switch (tolowerW(wHex))
119 return (wHex - '0') & 0xf;
126 return (wHex - 'a' + 10) & 0xf;
132 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
142 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
144 /* format: "offset, bytestocompare, mask, value" */
146 ulOffset = strtolW(wszPatternString, NULL, 10);
148 if (!(wszPatternString = strchrW(wszPatternString, ',')))
151 wszPatternString++; /* skip ',' */
153 ulBytes = strtolW(wszPatternString, NULL, 10);
155 pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
156 pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
157 pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
159 /* default mask is match everything */
160 memset(pbMask, 0xFF, ulBytes);
162 if (!(wszPatternString = strchrW(wszPatternString, ',')))
167 wszPatternString++; /* skip ',' */
168 while (!isxdigitW(*wszPatternString) && (*wszPatternString != ',')) wszPatternString++;
170 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
172 if ((strpos % 2) == 1) /* odd numbered position */
173 pbMask[strpos / 2] |= byte_from_hex_char(*wszPatternString);
175 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
178 if (!(wszPatternString = strchrW(wszPatternString, ',')))
181 wszPatternString++; /* skip ',' */
186 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
189 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
191 if ((strpos % 2) == 1) /* odd numbered position */
192 pbValue[strpos / 2] |= byte_from_hex_char(*wszPatternString);
194 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
199 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
204 for (i = 0; i < ulBytes; i++)
205 if ((pbFile[i] & pbMask[i]) != pbValue[i])
212 HeapFree(GetProcessHeap(), 0, pbMask);
213 HeapFree(GetProcessHeap(), 0, pbValue);
214 HeapFree(GetProcessHeap(), 0, pbFile);
216 /* if we encountered no errors with this string, and there is a following tuple, then we
217 * have to match that as well to succeed */
218 if ((hr == S_OK) && (wszPatternString = strchrW(wszPatternString, ',')))
219 return process_pattern_string(wszPatternString + 1, pReader);
224 static HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
226 HKEY hkeyMediaType = NULL;
230 static const WCHAR wszMediaType[] = {'M','e','d','i','a',' ','T','y','p','e',0};
232 TRACE("(%p, %s, %p, %p)\n", pReader, debugstr_w(pszFileName), majorType, minorType);
234 *majorType = GUID_NULL;
235 *minorType = GUID_NULL;
237 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType);
238 hr = HRESULT_FROM_WIN32(lRet);
244 for (indexMajor = 0; !bFound; indexMajor++)
247 WCHAR wszMajorKeyName[CHARS_IN_GUID];
248 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
249 static const WCHAR wszExtensions[] = {'E','x','t','e','n','s','i','o','n','s',0};
251 if (RegEnumKeyExW(hkeyMediaType, indexMajor, wszMajorKeyName, &dwKeyNameLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
253 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
255 TRACE("%s\n", debugstr_w(wszMajorKeyName));
256 if (!strcmpW(wszExtensions, wszMajorKeyName))
258 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType) == S_OK)
265 for (indexMinor = 0; !bFound; indexMinor++)
268 WCHAR wszMinorKeyName[CHARS_IN_GUID];
269 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
273 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
276 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
279 TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
281 if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
284 for (indexValue = 0; !bFound; indexValue++)
287 WCHAR wszValueName[14]; /* longest name we should encounter will be "Source Filter" */
288 LPWSTR wszPatternString = HeapAlloc(GetProcessHeap(), 0, maxValueLen);
289 DWORD dwValueNameLen = sizeof(wszValueName) / sizeof(wszValueName[0]); /* remember this is in chars */
290 DWORD dwDataLen = maxValueLen; /* remember this is in bytes */
291 static const WCHAR wszSourceFilter[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
294 if ((temp = RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen)) != ERROR_SUCCESS)
296 HeapFree(GetProcessHeap(), 0, wszPatternString);
300 /* if it is not the source filter value */
301 if (strcmpW(wszValueName, wszSourceFilter))
303 if (process_pattern_string(wszPatternString, pReader) == S_OK)
305 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName, majorType)) &&
306 SUCCEEDED(CLSIDFromString(wszMinorKeyName, minorType)))
310 HeapFree(GetProcessHeap(), 0, wszPatternString);
312 CloseHandle(hkeyMinor);
315 CloseHandle(hkeyMajor);
318 CloseHandle(hkeyMediaType);
320 if (SUCCEEDED(hr) && !bFound)
322 ERR("Media class not found\n");
326 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType), qzdebugstr_guid(minorType));
331 static IPin* WINAPI AsyncReader_GetPin(BaseFilter *iface, int pos)
333 AsyncReader *This = (AsyncReader *)iface;
335 if (pos >= 1 || !This->pOutputPin)
338 IPin_AddRef(This->pOutputPin);
339 return This->pOutputPin;
342 static LONG WINAPI AsyncReader_GetPinCount(BaseFilter *iface)
344 AsyncReader *This = (AsyncReader *)iface;
346 if (!This->pOutputPin)
352 static const BaseFilterFuncTable BaseFuncTable = {
354 AsyncReader_GetPinCount
357 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
359 AsyncReader *pAsyncRead;
362 return CLASS_E_NOAGGREGATION;
364 pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
367 return E_OUTOFMEMORY;
369 BaseFilter_Init(&pAsyncRead->filter, &AsyncReader_Vtbl, &CLSID_AsyncReader, (DWORD_PTR)(__FILE__ ": AsyncReader.csFilter"), &BaseFuncTable);
371 pAsyncRead->lpVtblFSF = &FileSource_Vtbl;
372 pAsyncRead->pOutputPin = NULL;
374 pAsyncRead->pszFileName = NULL;
375 pAsyncRead->pmt = NULL;
379 TRACE("-- created at %p\n", pAsyncRead);
384 /** IUnknown methods **/
386 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
388 AsyncReader *This = (AsyncReader *)iface;
390 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
394 if (IsEqualIID(riid, &IID_IUnknown))
396 else if (IsEqualIID(riid, &IID_IPersist))
398 else if (IsEqualIID(riid, &IID_IMediaFilter))
400 else if (IsEqualIID(riid, &IID_IBaseFilter))
402 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
403 *ppv = &This->lpVtblFSF;
407 IUnknown_AddRef((IUnknown *)(*ppv));
411 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking) &&
412 !IsEqualIID(riid, &IID_IVideoWindow) && !IsEqualIID(riid, &IID_IBasicAudio))
413 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
415 return E_NOINTERFACE;
418 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
420 AsyncReader *This = (AsyncReader *)iface;
421 ULONG refCount = BaseFilterImpl_Release(iface);
423 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
427 if (This->pOutputPin)
430 if(SUCCEEDED(IPin_ConnectedTo(This->pOutputPin, &pConnectedTo)))
432 IPin_Disconnect(pConnectedTo);
433 IPin_Release(pConnectedTo);
435 IPin_Disconnect(This->pOutputPin);
436 IPin_Release(This->pOutputPin);
438 CoTaskMemFree(This->pszFileName);
440 FreeMediaType(This->pmt);
448 /** IMediaFilter methods **/
450 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
452 AsyncReader *This = (AsyncReader *)iface;
456 This->filter.state = State_Stopped;
461 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
463 AsyncReader *This = (AsyncReader *)iface;
467 This->filter.state = State_Paused;
472 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
474 AsyncReader *This = (AsyncReader *)iface;
476 TRACE("(%x%08x)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
478 This->filter.state = State_Running;
483 /** IBaseFilter methods **/
485 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
487 FIXME("(%s, %p)\n", debugstr_w(Id), ppPin);
492 static const IBaseFilterVtbl AsyncReader_Vtbl =
494 AsyncReader_QueryInterface,
495 BaseFilterImpl_AddRef,
497 BaseFilterImpl_GetClassID,
501 BaseFilterImpl_GetState,
502 BaseFilterImpl_SetSyncSource,
503 BaseFilterImpl_GetSyncSource,
504 BaseFilterImpl_EnumPins,
506 BaseFilterImpl_QueryFilterInfo,
507 BaseFilterImpl_JoinFilterGraph,
508 BaseFilterImpl_QueryVendorInfo
511 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
513 AsyncReader *This = impl_from_IFileSourceFilter(iface);
515 return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->filter.lpVtbl, riid, ppv);
518 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
520 AsyncReader *This = impl_from_IFileSourceFilter(iface);
522 return IBaseFilter_AddRef((IFileSourceFilter*)&This->filter.lpVtbl);
525 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
527 AsyncReader *This = impl_from_IFileSourceFilter(iface);
529 return IBaseFilter_Release((IFileSourceFilter*)&This->filter.lpVtbl);
532 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
536 IAsyncReader * pReader = NULL;
537 AsyncReader *This = impl_from_IFileSourceFilter(iface);
539 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
542 /* FIXME: check the sharing values that native uses */
543 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
545 if (hFile == INVALID_HANDLE_VALUE)
547 return HRESULT_FROM_WIN32(GetLastError());
551 hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->filter.lpVtbl, &This->filter.csFilter, &This->pOutputPin);
552 BaseFilterImpl_IncrementPinVersion((BaseFilter *)&This->filter.lpVtbl);
555 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
557 /* store file name & media type */
560 CoTaskMemFree(This->pszFileName);
562 FreeMediaType(This->pmt);
564 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
565 strcpyW(This->pszFileName, pszFileName);
567 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
570 This->pmt->bFixedSizeSamples = TRUE;
571 This->pmt->bTemporalCompression = FALSE;
572 This->pmt->cbFormat = 0;
573 This->pmt->pbFormat = NULL;
574 This->pmt->pUnk = NULL;
575 This->pmt->lSampleSize = 0;
576 This->pmt->formattype = FORMAT_None;
577 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype);
580 CoTaskMemFree(This->pmt);
585 CopyMediaType(This->pmt, pmt);
589 IAsyncReader_Release(pReader);
593 if (This->pOutputPin)
595 IPin_Release(This->pOutputPin);
596 This->pOutputPin = NULL;
599 CoTaskMemFree(This->pszFileName);
601 FreeMediaType(This->pmt);
602 This->pszFileName = NULL;
608 /* FIXME: check return codes */
612 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
614 AsyncReader *This = impl_from_IFileSourceFilter(iface);
616 TRACE("(%p, %p)\n", ppszFileName, pmt);
621 /* copy file name & media type if available, otherwise clear the outputs */
622 if (This->pszFileName)
624 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
625 strcpyW(*ppszFileName, This->pszFileName);
628 *ppszFileName = NULL;
633 CopyMediaType(pmt, This->pmt);
635 ZeroMemory(pmt, sizeof(*pmt));
641 static const IFileSourceFilterVtbl FileSource_Vtbl =
643 FileSource_QueryInterface,
647 FileSource_GetCurFile
651 /* the dwUserData passed back to user */
652 typedef struct DATAREQUEST
654 IMediaSample * pSample; /* sample passed to us by user */
655 DWORD_PTR dwUserData; /* user data passed to us */
656 OVERLAPPED ovl; /* our overlapped structure */
659 typedef struct FileAsyncReader
662 const struct IAsyncReaderVtbl * lpVtblAR;
664 ALLOCATOR_PROPERTIES allocProps;
667 /* Why would you need more? Every sample has its own handle */
671 CRITICAL_SECTION csList; /* critical section to prevent concurrency issues */
672 DATAREQUEST *sample_list;
674 /* Have a handle for every sample, and then one more as flushing handle */
678 static inline FileAsyncReader *impl_from_IAsyncReader( IAsyncReader *iface )
680 return (FileAsyncReader *)((char*)iface - FIELD_OFFSET(FileAsyncReader, lpVtblAR));
683 static HRESULT WINAPI FileAsyncReaderPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *pmt)
685 FileAsyncReader *This = (FileAsyncReader *)iface;
686 AM_MEDIA_TYPE *pmt_filter = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
688 FIXME("(%p, %p)\n", iface, pmt);
690 if (IsEqualGUID(&pmt->majortype, &pmt_filter->majortype) &&
691 IsEqualGUID(&pmt->subtype, &pmt_filter->subtype) &&
692 IsEqualGUID(&pmt->formattype, &FORMAT_None))
698 static HRESULT WINAPI FileAsyncReaderPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
700 FileAsyncReader *This = (FileAsyncReader *)iface;
704 return VFW_S_NO_MORE_ITEMS;
705 CopyMediaType(pmt, ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt);
709 /* overridden pin functions */
711 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
713 FileAsyncReader *This = (FileAsyncReader *)iface;
714 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
718 if (IsEqualIID(riid, &IID_IUnknown))
720 else if (IsEqualIID(riid, &IID_IPin))
722 else if (IsEqualIID(riid, &IID_IAsyncReader))
723 *ppv = &This->lpVtblAR;
727 IUnknown_AddRef((IUnknown *)(*ppv));
731 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking))
732 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
734 return E_NOINTERFACE;
737 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
739 FileAsyncReader *This = (FileAsyncReader *)iface;
740 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
743 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
747 CoTaskMemFree(This->sample_list);
748 if (This->handle_list)
750 for (x = 0; x <= This->samples; ++x)
751 CloseHandle(This->handle_list[x]);
752 CoTaskMemFree(This->handle_list);
754 CloseHandle(This->hFile);
755 This->csList.DebugInfo->Spare[0] = 0;
756 DeleteCriticalSection(&This->csList);
763 static const IPinVtbl FileAsyncReaderPin_Vtbl =
765 FileAsyncReaderPin_QueryInterface,
767 FileAsyncReaderPin_Release,
768 BaseOutputPinImpl_Connect,
769 BaseOutputPinImpl_ReceiveConnection,
770 BasePinImpl_Disconnect,
771 BasePinImpl_ConnectedTo,
772 BasePinImpl_ConnectionMediaType,
773 BasePinImpl_QueryPinInfo,
774 BasePinImpl_QueryDirection,
776 FileAsyncReaderPin_QueryAccept,
777 BasePinImpl_EnumMediaTypes,
778 BasePinImpl_QueryInternalConnections,
779 BaseOutputPinImpl_EndOfStream,
780 BaseOutputPinImpl_BeginFlush,
781 BaseOutputPinImpl_EndFlush,
782 BaseOutputPinImpl_NewSegment
785 /* Function called as a helper to IPin_Connect */
786 /* specific AM_MEDIA_TYPE - it cannot be NULL */
787 /* this differs from standard OutputPin_AttemptConnection only in that it
788 * doesn't need the IMemInputPin interface on the receiving pin */
789 static HRESULT WINAPI FileAsyncReaderPin_AttemptConnection(BasePin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
791 BaseOutputPin *This = (BaseOutputPin *)iface;
794 TRACE("(%p, %p)\n", pReceivePin, pmt);
795 dump_AM_MEDIA_TYPE(pmt);
797 /* FIXME: call queryacceptproc */
799 This->pin.pConnectedTo = pReceivePin;
800 IPin_AddRef(pReceivePin);
801 CopyMediaType(&This->pin.mtCurrent, pmt);
803 hr = IPin_ReceiveConnection(pReceivePin, (IPin*)iface, pmt);
807 IPin_Release(This->pin.pConnectedTo);
808 This->pin.pConnectedTo = NULL;
809 FreeMediaType(&This->pin.mtCurrent);
812 TRACE(" -- %x\n", hr);
816 static HRESULT WINAPI FileAsyncReaderPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
818 FileAsyncReader *This = (FileAsyncReader *)iface;
819 ALLOCATOR_PROPERTIES actual;
821 if (ppropInputRequest->cbAlign && ppropInputRequest->cbAlign != This->allocProps.cbAlign)
822 FIXME("Requested Buffer cbAlign mismatch %i,%i\n",This->allocProps.cbAlign, ppropInputRequest->cbAlign);
823 if (ppropInputRequest->cbPrefix)
824 FIXME("Requested Buffer cbPrefix mismatch %i,%i\n",This->allocProps.cbPrefix, ppropInputRequest->cbPrefix);
825 if (ppropInputRequest->cbBuffer)
826 FIXME("Requested Buffer cbBuffer mismatch %i,%i\n",This->allocProps.cbBuffer, ppropInputRequest->cbBuffer);
827 if (ppropInputRequest->cBuffers)
828 FIXME("Requested Buffer cBuffers mismatch %i,%i\n",This->allocProps.cBuffers, ppropInputRequest->cBuffers);
830 return IMemAllocator_SetProperties(pAlloc, &This->allocProps, &actual);
833 static const BasePinFuncTable output_BaseFuncTable = {
835 FileAsyncReaderPin_AttemptConnection,
836 BasePinImpl_GetMediaTypeVersion,
837 FileAsyncReaderPin_GetMediaType
840 static const BaseOutputPinFuncTable output_BaseOutputFuncTable = {
841 FileAsyncReaderPin_DecideBufferSize,
842 BaseOutputPinImpl_DecideAllocator,
843 BaseOutputPinImpl_BreakConnect
846 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
852 piOutput.dir = PINDIR_OUTPUT;
853 piOutput.pFilter = pBaseFilter;
854 strcpyW(piOutput.achName, wszOutputPinName);
855 hr = BaseOutputPin_Construct(&FileAsyncReaderPin_Vtbl, sizeof(FileAsyncReader), &piOutput, &output_BaseFuncTable, &output_BaseOutputFuncTable, pCritSec, ppPin);
859 FileAsyncReader *pPinImpl = (FileAsyncReader *)*ppPin;
860 pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
861 pPinImpl->hFile = hFile;
862 pPinImpl->bFlushing = FALSE;
863 pPinImpl->sample_list = NULL;
864 pPinImpl->handle_list = NULL;
865 pPinImpl->queued_number = 0;
866 InitializeCriticalSection(&pPinImpl->csList);
867 pPinImpl->csList.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FileAsyncReader.csList");
874 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
876 FileAsyncReader *This = impl_from_IAsyncReader(iface);
878 return IPin_QueryInterface((IPin *)This, riid, ppv);
881 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
883 FileAsyncReader *This = impl_from_IAsyncReader(iface);
885 return IPin_AddRef((IPin *)This);
888 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
890 FileAsyncReader *This = impl_from_IAsyncReader(iface);
892 return IPin_Release((IPin *)This);
895 #define DEF_ALIGNMENT 1
897 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
899 FileAsyncReader *This = impl_from_IAsyncReader(iface);
903 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
905 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
906 pProps->cbAlign = DEF_ALIGNMENT;
910 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
911 /* FIXME: check we are still aligned */
914 IMemAllocator_AddRef(pPreferred);
915 *ppActual = pPreferred;
916 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
923 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
927 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
928 /* FIXME: check we are still aligned */
931 *ppActual = pPreferred;
932 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
939 CoTaskMemFree(This->sample_list);
940 if (This->handle_list)
943 for (x = 0; x <= This->samples; ++x)
944 CloseHandle(This->handle_list[x]);
945 CoTaskMemFree(This->handle_list);
948 This->samples = pProps->cBuffers;
949 This->oldest_sample = 0;
950 TRACE("Samples: %u\n", This->samples);
951 This->sample_list = CoTaskMemAlloc(sizeof(This->sample_list[0]) * pProps->cBuffers);
952 This->handle_list = CoTaskMemAlloc(sizeof(HANDLE) * pProps->cBuffers * 2);
954 if (This->sample_list && This->handle_list)
957 ZeroMemory(This->sample_list, sizeof(This->sample_list[0]) * pProps->cBuffers);
958 for (x = 0; x < This->samples; ++x)
960 This->sample_list[x].ovl.hEvent = This->handle_list[x] = CreateEventW(NULL, 0, 0, NULL);
961 if (x + 1 < This->samples)
962 This->handle_list[This->samples + 1 + x] = This->handle_list[x];
964 This->handle_list[This->samples] = CreateEventW(NULL, 1, 0, NULL);
965 This->allocProps = *pProps;
970 CoTaskMemFree(This->sample_list);
971 CoTaskMemFree(This->handle_list);
973 This->sample_list = NULL;
974 This->handle_list = NULL;
982 IMemAllocator_Release(pPreferred);
985 TRACE("-- %x\n", hr);
989 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
990 * however, this would be quite complicated to do and may be a bit error prone */
991 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
994 REFERENCE_TIME Start;
996 FileAsyncReader *This = impl_from_IAsyncReader(iface);
997 LPBYTE pBuffer = NULL;
999 TRACE("(%p, %lx)\n", pSample, dwUser);
1004 /* get start and stop positions in bytes */
1006 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
1009 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1011 EnterCriticalSection(&This->csList);
1012 if (This->bFlushing)
1014 LeaveCriticalSection(&This->csList);
1015 return VFW_E_WRONG_STATE;
1020 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
1021 DATAREQUEST *pDataRq;
1024 /* Try to insert above the waiting sample if possible */
1025 for (x = This->oldest_sample; x < This->samples; ++x)
1027 if (!This->sample_list[x].pSample)
1031 if (x >= This->samples)
1032 for (x = 0; x < This->oldest_sample; ++x)
1034 if (!This->sample_list[x].pSample)
1038 /* There must be a sample we have found */
1039 assert(x < This->samples);
1040 ++This->queued_number;
1042 pDataRq = This->sample_list + x;
1044 pDataRq->ovl.u.s.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
1045 pDataRq->ovl.u.s.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
1046 pDataRq->dwUserData = dwUser;
1048 /* we violate traditional COM rules here by maintaining
1049 * a reference to the sample, but not calling AddRef, but
1050 * that's what MSDN says to do */
1051 pDataRq->pSample = pSample;
1053 /* this is definitely not how it is implemented on Win9x
1054 * as they do not support async reads on files, but it is
1055 * sooo much easier to use this than messing around with threads!
1057 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1058 hr = HRESULT_FROM_WIN32(GetLastError());
1060 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1061 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1065 LeaveCriticalSection(&This->csList);
1067 TRACE("-- %x\n", hr);
1071 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1074 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1077 TRACE("(%u, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1082 EnterCriticalSection(&This->csList);
1083 if (!This->bFlushing)
1085 LONG oldest = This->oldest_sample;
1087 if (!This->queued_number)
1089 /* It could be that nothing is queued right now, but that can be fixed */
1090 WARN("Called without samples in queue and not flushing!!\n");
1092 LeaveCriticalSection(&This->csList);
1094 /* wait for an object to read, or time out */
1095 buffer = WaitForMultipleObjectsEx(This->samples+1, This->handle_list + oldest, FALSE, dwTimeout, TRUE);
1097 EnterCriticalSection(&This->csList);
1098 if (buffer <= This->samples)
1100 /* Re-scale the buffer back to normal */
1103 /* Uh oh, we overshot the flusher handle, renormalize it back to 0..Samples-1 */
1104 if (buffer > This->samples)
1105 buffer -= This->samples + 1;
1106 assert(buffer <= This->samples);
1109 if (buffer >= This->samples)
1111 if (buffer != This->samples)
1113 FIXME("Returned: %u (%08x)\n", buffer, GetLastError());
1117 hr = VFW_E_WRONG_STATE;
1121 --This->queued_number;
1124 if (This->bFlushing && buffer == ~0)
1126 for (buffer = 0; buffer < This->samples; ++buffer)
1128 if (This->sample_list[buffer].pSample)
1130 ResetEvent(This->handle_list[buffer]);
1134 if (buffer == This->samples)
1136 assert(!This->queued_number);
1141 --This->queued_number;
1148 REFERENCE_TIME rtStart, rtStop;
1149 REFERENCE_TIME rtSampleStart, rtSampleStop;
1150 DATAREQUEST *pDataRq = This->sample_list + buffer;
1153 /* get any errors */
1154 if (!This->bFlushing && !GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1155 hr = HRESULT_FROM_WIN32(GetLastError());
1157 /* Return the sample no matter what so it can be destroyed */
1158 *ppSample = pDataRq->pSample;
1159 *pdwUser = pDataRq->dwUserData;
1161 if (This->bFlushing)
1162 hr = VFW_E_WRONG_STATE;
1167 /* Set the time on the sample */
1168 IMediaSample_SetActualDataLength(pDataRq->pSample, dwBytes);
1170 rtStart = (DWORD64)pDataRq->ovl.u.s.Offset + ((DWORD64)pDataRq->ovl.u.s.OffsetHigh << 32);
1171 rtStart = MEDIATIME_FROM_BYTES(rtStart);
1172 rtStop = rtStart + MEDIATIME_FROM_BYTES(dwBytes);
1174 IMediaSample_GetTime(pDataRq->pSample, &rtSampleStart, &rtSampleStop);
1175 assert(rtStart == rtSampleStart);
1176 assert(rtStop <= rtSampleStop);
1178 IMediaSample_SetTime(pDataRq->pSample, &rtStart, &rtStop);
1179 assert(rtStart == rtSampleStart);
1181 assert(rtStop == rtSampleStop);
1183 assert(rtStop == rtStart);
1185 This->sample_list[buffer].pSample = NULL;
1186 assert(This->oldest_sample < This->samples);
1188 if (buffer == This->oldest_sample)
1191 for (x = This->oldest_sample + 1; x < This->samples; ++x)
1192 if (This->sample_list[x].pSample)
1194 if (x >= This->samples)
1195 for (x = 0; x < This->oldest_sample; ++x)
1196 if (This->sample_list[x].pSample)
1198 if (This->oldest_sample == x)
1199 /* No samples found, reset to 0 */
1201 This->oldest_sample = x;
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 SetEvent(This->handle_list[This->samples]);
1300 LeaveCriticalSection(&This->csList);
1305 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1307 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1312 EnterCriticalSection(&This->csList);
1313 ResetEvent(This->handle_list[This->samples]);
1314 This->bFlushing = FALSE;
1315 for (x = 0; x < This->samples; ++x)
1316 assert(!This->sample_list[x].pSample);
1318 LeaveCriticalSection(&This->csList);
1323 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1325 FileAsyncReader_QueryInterface,
1326 FileAsyncReader_AddRef,
1327 FileAsyncReader_Release,
1328 FileAsyncReader_RequestAllocator,
1329 FileAsyncReader_Request,
1330 FileAsyncReader_WaitForNext,
1331 FileAsyncReader_SyncReadAligned,
1332 FileAsyncReader_SyncRead,
1333 FileAsyncReader_Length,
1334 FileAsyncReader_BeginFlush,
1335 FileAsyncReader_EndFlush,