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;
44 const IAMFilterMiscFlagsVtbl * IAMFilterMiscFlags_vtbl;
51 static const IBaseFilterVtbl AsyncReader_Vtbl;
52 static const IFileSourceFilterVtbl FileSource_Vtbl;
53 static const IAsyncReaderVtbl FileAsyncReader_Vtbl;
54 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl;
56 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
58 static inline AsyncReader *impl_from_IFileSourceFilter( IFileSourceFilter *iface )
60 return (AsyncReader *)((char*)iface - FIELD_OFFSET(AsyncReader, lpVtblFSF));
63 static WCHAR const mediatype_name[11] = {
64 'M', 'e', 'd', 'i', 'a', ' ', 'T', 'y', 'p', 'e', 0 };
65 static WCHAR const subtype_name[8] = {
66 'S', 'u', 'b', 't', 'y', 'p', 'e', 0 };
68 static HRESULT process_extensions(HKEY hkeyExtensions, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
79 /* Get the part of the name that matters */
80 extension = PathFindExtensionW(pszFileName);
81 if (*extension != '.')
84 l = RegOpenKeyExW(hkeyExtensions, extension, 0, KEY_READ, &hsub);
88 size = sizeof(keying);
89 l = RegQueryValueExW(hsub, mediatype_name, NULL, NULL, (LPBYTE)keying, &size);
91 CLSIDFromString(keying, majorType);
93 size = sizeof(keying);
95 l = RegQueryValueExW(hsub, subtype_name, NULL, NULL, (LPBYTE)keying, &size);
98 CLSIDFromString(keying, minorType);
107 static unsigned char byte_from_hex_char(WCHAR wHex)
109 switch (tolowerW(wHex))
121 return (wHex - '0') & 0xf;
128 return (wHex - 'a' + 10) & 0xf;
134 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
144 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
146 /* format: "offset, bytestocompare, mask, value" */
148 ulOffset = strtolW(wszPatternString, NULL, 10);
150 if (!(wszPatternString = strchrW(wszPatternString, ',')))
153 wszPatternString++; /* skip ',' */
155 ulBytes = strtolW(wszPatternString, NULL, 10);
157 pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
158 pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
159 pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
161 /* default mask is match everything */
162 memset(pbMask, 0xFF, ulBytes);
164 if (!(wszPatternString = strchrW(wszPatternString, ',')))
169 wszPatternString++; /* skip ',' */
170 while (!isxdigitW(*wszPatternString) && (*wszPatternString != ',')) wszPatternString++;
172 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
174 if ((strpos % 2) == 1) /* odd numbered position */
175 pbMask[strpos / 2] |= byte_from_hex_char(*wszPatternString);
177 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
180 if (!(wszPatternString = strchrW(wszPatternString, ',')))
183 wszPatternString++; /* skip ',' */
188 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
191 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
193 if ((strpos % 2) == 1) /* odd numbered position */
194 pbValue[strpos / 2] |= byte_from_hex_char(*wszPatternString);
196 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
201 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
206 for (i = 0; i < ulBytes; i++)
207 if ((pbFile[i] & pbMask[i]) != pbValue[i])
214 HeapFree(GetProcessHeap(), 0, pbMask);
215 HeapFree(GetProcessHeap(), 0, pbValue);
216 HeapFree(GetProcessHeap(), 0, pbFile);
218 /* if we encountered no errors with this string, and there is a following tuple, then we
219 * have to match that as well to succeed */
220 if ((hr == S_OK) && (wszPatternString = strchrW(wszPatternString, ',')))
221 return process_pattern_string(wszPatternString + 1, pReader);
226 static HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
228 HKEY hkeyMediaType = NULL;
232 static const WCHAR wszMediaType[] = {'M','e','d','i','a',' ','T','y','p','e',0};
234 TRACE("(%p, %s, %p, %p)\n", pReader, debugstr_w(pszFileName), majorType, minorType);
236 *majorType = GUID_NULL;
237 *minorType = GUID_NULL;
239 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType);
240 hr = HRESULT_FROM_WIN32(lRet);
246 for (indexMajor = 0; !bFound; indexMajor++)
249 WCHAR wszMajorKeyName[CHARS_IN_GUID];
250 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
251 static const WCHAR wszExtensions[] = {'E','x','t','e','n','s','i','o','n','s',0};
253 if (RegEnumKeyExW(hkeyMediaType, indexMajor, wszMajorKeyName, &dwKeyNameLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
255 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
257 TRACE("%s\n", debugstr_w(wszMajorKeyName));
258 if (!strcmpW(wszExtensions, wszMajorKeyName))
260 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType) == S_OK)
267 for (indexMinor = 0; !bFound; indexMinor++)
270 WCHAR wszMinorKeyName[CHARS_IN_GUID];
271 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
275 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
278 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
281 TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
283 if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
286 for (indexValue = 0; !bFound; indexValue++)
289 WCHAR wszValueName[14]; /* longest name we should encounter will be "Source Filter" */
290 LPWSTR wszPatternString = HeapAlloc(GetProcessHeap(), 0, maxValueLen);
291 DWORD dwValueNameLen = sizeof(wszValueName) / sizeof(wszValueName[0]); /* remember this is in chars */
292 DWORD dwDataLen = maxValueLen; /* remember this is in bytes */
293 static const WCHAR wszSourceFilter[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
296 if ((temp = RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen)) != ERROR_SUCCESS)
298 HeapFree(GetProcessHeap(), 0, wszPatternString);
302 /* if it is not the source filter value */
303 if (strcmpW(wszValueName, wszSourceFilter))
305 if (process_pattern_string(wszPatternString, pReader) == S_OK)
307 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName, majorType)) &&
308 SUCCEEDED(CLSIDFromString(wszMinorKeyName, minorType)))
312 HeapFree(GetProcessHeap(), 0, wszPatternString);
314 CloseHandle(hkeyMinor);
317 CloseHandle(hkeyMajor);
320 CloseHandle(hkeyMediaType);
322 if (SUCCEEDED(hr) && !bFound)
324 ERR("Media class not found\n");
328 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType), qzdebugstr_guid(minorType));
333 static IPin* WINAPI AsyncReader_GetPin(BaseFilter *iface, int pos)
335 AsyncReader *This = (AsyncReader *)iface;
337 if (pos >= 1 || !This->pOutputPin)
340 IPin_AddRef(This->pOutputPin);
341 return This->pOutputPin;
344 static LONG WINAPI AsyncReader_GetPinCount(BaseFilter *iface)
346 AsyncReader *This = (AsyncReader *)iface;
348 if (!This->pOutputPin)
354 static const BaseFilterFuncTable BaseFuncTable = {
356 AsyncReader_GetPinCount
359 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
361 AsyncReader *pAsyncRead;
364 return CLASS_E_NOAGGREGATION;
366 pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
369 return E_OUTOFMEMORY;
371 BaseFilter_Init(&pAsyncRead->filter, &AsyncReader_Vtbl, &CLSID_AsyncReader, (DWORD_PTR)(__FILE__ ": AsyncReader.csFilter"), &BaseFuncTable);
373 pAsyncRead->lpVtblFSF = &FileSource_Vtbl;
374 pAsyncRead->IAMFilterMiscFlags_vtbl = &IAMFilterMiscFlags_Vtbl;
375 pAsyncRead->pOutputPin = NULL;
377 pAsyncRead->pszFileName = NULL;
378 pAsyncRead->pmt = NULL;
382 TRACE("-- created at %p\n", pAsyncRead);
387 /** IUnknown methods **/
389 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
391 AsyncReader *This = (AsyncReader *)iface;
393 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
397 if (IsEqualIID(riid, &IID_IUnknown))
399 else if (IsEqualIID(riid, &IID_IPersist))
401 else if (IsEqualIID(riid, &IID_IMediaFilter))
403 else if (IsEqualIID(riid, &IID_IBaseFilter))
405 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
406 *ppv = &This->lpVtblFSF;
407 else if (IsEqualIID(riid, &IID_IAMFilterMiscFlags))
408 *ppv = &This->IAMFilterMiscFlags_vtbl;
412 IUnknown_AddRef((IUnknown *)(*ppv));
416 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking) &&
417 !IsEqualIID(riid, &IID_IVideoWindow) && !IsEqualIID(riid, &IID_IBasicAudio))
418 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
420 return E_NOINTERFACE;
423 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
425 AsyncReader *This = (AsyncReader *)iface;
426 ULONG refCount = BaseFilterImpl_Release(iface);
428 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
432 if (This->pOutputPin)
435 if(SUCCEEDED(IPin_ConnectedTo(This->pOutputPin, &pConnectedTo)))
437 IPin_Disconnect(pConnectedTo);
438 IPin_Release(pConnectedTo);
440 IPin_Disconnect(This->pOutputPin);
441 IPin_Release(This->pOutputPin);
443 CoTaskMemFree(This->pszFileName);
445 FreeMediaType(This->pmt);
453 /** IMediaFilter methods **/
455 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
457 AsyncReader *This = (AsyncReader *)iface;
461 This->filter.state = State_Stopped;
466 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
468 AsyncReader *This = (AsyncReader *)iface;
472 This->filter.state = State_Paused;
477 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
479 AsyncReader *This = (AsyncReader *)iface;
481 TRACE("(%x%08x)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
483 This->filter.state = State_Running;
488 /** IBaseFilter methods **/
490 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
492 AsyncReader *This = (AsyncReader *)iface;
493 TRACE("(%s, %p)\n", debugstr_w(Id), ppPin);
498 if (strcmpW(Id, wszOutputPinName))
501 return VFW_E_NOT_FOUND;
504 *ppPin = (IPin*)This->pOutputPin;
505 IUnknown_AddRef(*ppPin);
509 static const IBaseFilterVtbl AsyncReader_Vtbl =
511 AsyncReader_QueryInterface,
512 BaseFilterImpl_AddRef,
514 BaseFilterImpl_GetClassID,
518 BaseFilterImpl_GetState,
519 BaseFilterImpl_SetSyncSource,
520 BaseFilterImpl_GetSyncSource,
521 BaseFilterImpl_EnumPins,
523 BaseFilterImpl_QueryFilterInfo,
524 BaseFilterImpl_JoinFilterGraph,
525 BaseFilterImpl_QueryVendorInfo
528 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
530 AsyncReader *This = impl_from_IFileSourceFilter(iface);
532 return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->filter.lpVtbl, riid, ppv);
535 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
537 AsyncReader *This = impl_from_IFileSourceFilter(iface);
539 return IBaseFilter_AddRef((IFileSourceFilter*)&This->filter.lpVtbl);
542 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
544 AsyncReader *This = impl_from_IFileSourceFilter(iface);
546 return IBaseFilter_Release((IFileSourceFilter*)&This->filter.lpVtbl);
549 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
553 IAsyncReader * pReader = NULL;
554 AsyncReader *This = impl_from_IFileSourceFilter(iface);
556 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
559 /* FIXME: check the sharing values that native uses */
560 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
562 if (hFile == INVALID_HANDLE_VALUE)
564 return HRESULT_FROM_WIN32(GetLastError());
568 hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->filter.lpVtbl, &This->filter.csFilter, &This->pOutputPin);
569 BaseFilterImpl_IncrementPinVersion((BaseFilter *)&This->filter.lpVtbl);
572 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
574 /* store file name & media type */
577 CoTaskMemFree(This->pszFileName);
579 FreeMediaType(This->pmt);
581 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
582 strcpyW(This->pszFileName, pszFileName);
584 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
587 This->pmt->bFixedSizeSamples = TRUE;
588 This->pmt->bTemporalCompression = FALSE;
589 This->pmt->cbFormat = 0;
590 This->pmt->pbFormat = NULL;
591 This->pmt->pUnk = NULL;
592 This->pmt->lSampleSize = 0;
593 This->pmt->formattype = FORMAT_None;
594 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype);
597 CoTaskMemFree(This->pmt);
602 CopyMediaType(This->pmt, pmt);
606 IAsyncReader_Release(pReader);
610 if (This->pOutputPin)
612 IPin_Release(This->pOutputPin);
613 This->pOutputPin = NULL;
616 CoTaskMemFree(This->pszFileName);
618 FreeMediaType(This->pmt);
619 This->pszFileName = NULL;
625 /* FIXME: check return codes */
629 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
631 AsyncReader *This = impl_from_IFileSourceFilter(iface);
633 TRACE("(%p, %p)\n", ppszFileName, pmt);
638 /* copy file name & media type if available, otherwise clear the outputs */
639 if (This->pszFileName)
641 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
642 strcpyW(*ppszFileName, This->pszFileName);
645 *ppszFileName = NULL;
650 CopyMediaType(pmt, This->pmt);
652 ZeroMemory(pmt, sizeof(*pmt));
658 static const IFileSourceFilterVtbl FileSource_Vtbl =
660 FileSource_QueryInterface,
664 FileSource_GetCurFile
668 /* the dwUserData passed back to user */
669 typedef struct DATAREQUEST
671 IMediaSample * pSample; /* sample passed to us by user */
672 DWORD_PTR dwUserData; /* user data passed to us */
673 OVERLAPPED ovl; /* our overlapped structure */
676 typedef struct FileAsyncReader
679 const struct IAsyncReaderVtbl * lpVtblAR;
681 ALLOCATOR_PROPERTIES allocProps;
684 /* Why would you need more? Every sample has its own handle */
688 CRITICAL_SECTION csList; /* critical section to prevent concurrency issues */
689 DATAREQUEST *sample_list;
691 /* Have a handle for every sample, and then one more as flushing handle */
695 static inline FileAsyncReader *impl_from_IAsyncReader( IAsyncReader *iface )
697 return (FileAsyncReader *)((char*)iface - FIELD_OFFSET(FileAsyncReader, lpVtblAR));
700 static HRESULT WINAPI FileAsyncReaderPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *pmt)
702 FileAsyncReader *This = (FileAsyncReader *)iface;
703 AM_MEDIA_TYPE *pmt_filter = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
705 FIXME("(%p, %p)\n", iface, pmt);
707 if (IsEqualGUID(&pmt->majortype, &pmt_filter->majortype) &&
708 IsEqualGUID(&pmt->subtype, &pmt_filter->subtype) &&
709 IsEqualGUID(&pmt->formattype, &FORMAT_None))
715 static HRESULT WINAPI FileAsyncReaderPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
717 FileAsyncReader *This = (FileAsyncReader *)iface;
721 return VFW_S_NO_MORE_ITEMS;
722 CopyMediaType(pmt, ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt);
726 /* overridden pin functions */
728 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
730 FileAsyncReader *This = (FileAsyncReader *)iface;
731 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
735 if (IsEqualIID(riid, &IID_IUnknown))
737 else if (IsEqualIID(riid, &IID_IPin))
739 else if (IsEqualIID(riid, &IID_IAsyncReader))
740 *ppv = &This->lpVtblAR;
744 IUnknown_AddRef((IUnknown *)(*ppv));
748 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking))
749 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
751 return E_NOINTERFACE;
754 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
756 FileAsyncReader *This = (FileAsyncReader *)iface;
757 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
760 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
764 CoTaskMemFree(This->sample_list);
765 if (This->handle_list)
767 for (x = 0; x <= This->samples; ++x)
768 CloseHandle(This->handle_list[x]);
769 CoTaskMemFree(This->handle_list);
771 CloseHandle(This->hFile);
772 This->csList.DebugInfo->Spare[0] = 0;
773 DeleteCriticalSection(&This->csList);
780 static const IPinVtbl FileAsyncReaderPin_Vtbl =
782 FileAsyncReaderPin_QueryInterface,
784 FileAsyncReaderPin_Release,
785 BaseOutputPinImpl_Connect,
786 BaseOutputPinImpl_ReceiveConnection,
787 BasePinImpl_Disconnect,
788 BasePinImpl_ConnectedTo,
789 BasePinImpl_ConnectionMediaType,
790 BasePinImpl_QueryPinInfo,
791 BasePinImpl_QueryDirection,
793 FileAsyncReaderPin_QueryAccept,
794 BasePinImpl_EnumMediaTypes,
795 BasePinImpl_QueryInternalConnections,
796 BaseOutputPinImpl_EndOfStream,
797 BaseOutputPinImpl_BeginFlush,
798 BaseOutputPinImpl_EndFlush,
799 BasePinImpl_NewSegment
802 /* Function called as a helper to IPin_Connect */
803 /* specific AM_MEDIA_TYPE - it cannot be NULL */
804 /* this differs from standard OutputPin_AttemptConnection only in that it
805 * doesn't need the IMemInputPin interface on the receiving pin */
806 static HRESULT WINAPI FileAsyncReaderPin_AttemptConnection(BasePin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
808 BaseOutputPin *This = (BaseOutputPin *)iface;
811 TRACE("(%p, %p)\n", pReceivePin, pmt);
812 dump_AM_MEDIA_TYPE(pmt);
814 /* FIXME: call queryacceptproc */
816 This->pin.pConnectedTo = pReceivePin;
817 IPin_AddRef(pReceivePin);
818 CopyMediaType(&This->pin.mtCurrent, pmt);
820 hr = IPin_ReceiveConnection(pReceivePin, (IPin*)iface, pmt);
824 IPin_Release(This->pin.pConnectedTo);
825 This->pin.pConnectedTo = NULL;
826 FreeMediaType(&This->pin.mtCurrent);
829 TRACE(" -- %x\n", hr);
833 static HRESULT WINAPI FileAsyncReaderPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
835 FileAsyncReader *This = (FileAsyncReader *)iface;
836 ALLOCATOR_PROPERTIES actual;
838 if (ppropInputRequest->cbAlign && ppropInputRequest->cbAlign != This->allocProps.cbAlign)
839 FIXME("Requested Buffer cbAlign mismatch %i,%i\n",This->allocProps.cbAlign, ppropInputRequest->cbAlign);
840 if (ppropInputRequest->cbPrefix)
841 FIXME("Requested Buffer cbPrefix mismatch %i,%i\n",This->allocProps.cbPrefix, ppropInputRequest->cbPrefix);
842 if (ppropInputRequest->cbBuffer)
843 FIXME("Requested Buffer cbBuffer mismatch %i,%i\n",This->allocProps.cbBuffer, ppropInputRequest->cbBuffer);
844 if (ppropInputRequest->cBuffers)
845 FIXME("Requested Buffer cBuffers mismatch %i,%i\n",This->allocProps.cBuffers, ppropInputRequest->cBuffers);
847 return IMemAllocator_SetProperties(pAlloc, &This->allocProps, &actual);
850 static const BasePinFuncTable output_BaseFuncTable = {
852 FileAsyncReaderPin_AttemptConnection,
853 BasePinImpl_GetMediaTypeVersion,
854 FileAsyncReaderPin_GetMediaType
857 static const BaseOutputPinFuncTable output_BaseOutputFuncTable = {
858 FileAsyncReaderPin_DecideBufferSize,
859 BaseOutputPinImpl_DecideAllocator,
860 BaseOutputPinImpl_BreakConnect
863 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
869 piOutput.dir = PINDIR_OUTPUT;
870 piOutput.pFilter = pBaseFilter;
871 strcpyW(piOutput.achName, wszOutputPinName);
872 hr = BaseOutputPin_Construct(&FileAsyncReaderPin_Vtbl, sizeof(FileAsyncReader), &piOutput, &output_BaseFuncTable, &output_BaseOutputFuncTable, pCritSec, ppPin);
876 FileAsyncReader *pPinImpl = (FileAsyncReader *)*ppPin;
877 pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
878 pPinImpl->hFile = hFile;
879 pPinImpl->bFlushing = FALSE;
880 pPinImpl->sample_list = NULL;
881 pPinImpl->handle_list = NULL;
882 pPinImpl->queued_number = 0;
883 InitializeCriticalSection(&pPinImpl->csList);
884 pPinImpl->csList.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FileAsyncReader.csList");
891 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
893 FileAsyncReader *This = impl_from_IAsyncReader(iface);
895 return IPin_QueryInterface((IPin *)This, riid, ppv);
898 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
900 FileAsyncReader *This = impl_from_IAsyncReader(iface);
902 return IPin_AddRef((IPin *)This);
905 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
907 FileAsyncReader *This = impl_from_IAsyncReader(iface);
909 return IPin_Release((IPin *)This);
912 #define DEF_ALIGNMENT 1
914 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
916 FileAsyncReader *This = impl_from_IAsyncReader(iface);
920 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
922 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
923 pProps->cbAlign = DEF_ALIGNMENT;
927 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
928 /* FIXME: check we are still aligned */
931 IMemAllocator_AddRef(pPreferred);
932 *ppActual = pPreferred;
933 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
940 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
944 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
945 /* FIXME: check we are still aligned */
948 *ppActual = pPreferred;
949 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
956 CoTaskMemFree(This->sample_list);
957 if (This->handle_list)
960 for (x = 0; x <= This->samples; ++x)
961 CloseHandle(This->handle_list[x]);
962 CoTaskMemFree(This->handle_list);
965 This->samples = pProps->cBuffers;
966 This->oldest_sample = 0;
967 TRACE("Samples: %u\n", This->samples);
968 This->sample_list = CoTaskMemAlloc(sizeof(This->sample_list[0]) * pProps->cBuffers);
969 This->handle_list = CoTaskMemAlloc(sizeof(HANDLE) * pProps->cBuffers * 2);
971 if (This->sample_list && This->handle_list)
974 ZeroMemory(This->sample_list, sizeof(This->sample_list[0]) * pProps->cBuffers);
975 for (x = 0; x < This->samples; ++x)
977 This->sample_list[x].ovl.hEvent = This->handle_list[x] = CreateEventW(NULL, 0, 0, NULL);
978 if (x + 1 < This->samples)
979 This->handle_list[This->samples + 1 + x] = This->handle_list[x];
981 This->handle_list[This->samples] = CreateEventW(NULL, 1, 0, NULL);
982 This->allocProps = *pProps;
987 CoTaskMemFree(This->sample_list);
988 CoTaskMemFree(This->handle_list);
990 This->sample_list = NULL;
991 This->handle_list = NULL;
999 IMemAllocator_Release(pPreferred);
1002 TRACE("-- %x\n", hr);
1006 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
1007 * however, this would be quite complicated to do and may be a bit error prone */
1008 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
1011 REFERENCE_TIME Start;
1012 REFERENCE_TIME Stop;
1013 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1014 LPBYTE pBuffer = NULL;
1016 TRACE("(%p, %lx)\n", pSample, dwUser);
1021 /* get start and stop positions in bytes */
1023 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
1026 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1028 EnterCriticalSection(&This->csList);
1029 if (This->bFlushing)
1031 LeaveCriticalSection(&This->csList);
1032 return VFW_E_WRONG_STATE;
1037 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
1038 DATAREQUEST *pDataRq;
1041 /* Try to insert above the waiting sample if possible */
1042 for (x = This->oldest_sample; x < This->samples; ++x)
1044 if (!This->sample_list[x].pSample)
1048 if (x >= This->samples)
1049 for (x = 0; x < This->oldest_sample; ++x)
1051 if (!This->sample_list[x].pSample)
1055 /* There must be a sample we have found */
1056 assert(x < This->samples);
1057 ++This->queued_number;
1059 pDataRq = This->sample_list + x;
1061 pDataRq->ovl.u.s.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
1062 pDataRq->ovl.u.s.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
1063 pDataRq->dwUserData = dwUser;
1065 /* we violate traditional COM rules here by maintaining
1066 * a reference to the sample, but not calling AddRef, but
1067 * that's what MSDN says to do */
1068 pDataRq->pSample = pSample;
1070 /* this is definitely not how it is implemented on Win9x
1071 * as they do not support async reads on files, but it is
1072 * sooo much easier to use this than messing around with threads!
1074 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1075 hr = HRESULT_FROM_WIN32(GetLastError());
1077 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1078 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1082 LeaveCriticalSection(&This->csList);
1084 TRACE("-- %x\n", hr);
1088 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1091 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1094 TRACE("(%u, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1099 EnterCriticalSection(&This->csList);
1100 if (!This->bFlushing)
1102 LONG oldest = This->oldest_sample;
1104 if (!This->queued_number)
1106 /* It could be that nothing is queued right now, but that can be fixed */
1107 WARN("Called without samples in queue and not flushing!!\n");
1109 LeaveCriticalSection(&This->csList);
1111 /* wait for an object to read, or time out */
1112 buffer = WaitForMultipleObjectsEx(This->samples+1, This->handle_list + oldest, FALSE, dwTimeout, TRUE);
1114 EnterCriticalSection(&This->csList);
1115 if (buffer <= This->samples)
1117 /* Re-scale the buffer back to normal */
1120 /* Uh oh, we overshot the flusher handle, renormalize it back to 0..Samples-1 */
1121 if (buffer > This->samples)
1122 buffer -= This->samples + 1;
1123 assert(buffer <= This->samples);
1126 if (buffer >= This->samples)
1128 if (buffer != This->samples)
1130 FIXME("Returned: %u (%08x)\n", buffer, GetLastError());
1134 hr = VFW_E_WRONG_STATE;
1138 --This->queued_number;
1141 if (This->bFlushing && buffer == ~0)
1143 for (buffer = 0; buffer < This->samples; ++buffer)
1145 if (This->sample_list[buffer].pSample)
1147 ResetEvent(This->handle_list[buffer]);
1151 if (buffer == This->samples)
1153 assert(!This->queued_number);
1158 --This->queued_number;
1165 REFERENCE_TIME rtStart, rtStop;
1166 REFERENCE_TIME rtSampleStart, rtSampleStop;
1167 DATAREQUEST *pDataRq = This->sample_list + buffer;
1170 /* get any errors */
1171 if (!This->bFlushing && !GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1172 hr = HRESULT_FROM_WIN32(GetLastError());
1174 /* Return the sample no matter what so it can be destroyed */
1175 *ppSample = pDataRq->pSample;
1176 *pdwUser = pDataRq->dwUserData;
1178 if (This->bFlushing)
1179 hr = VFW_E_WRONG_STATE;
1184 /* Set the time on the sample */
1185 IMediaSample_SetActualDataLength(pDataRq->pSample, dwBytes);
1187 rtStart = (DWORD64)pDataRq->ovl.u.s.Offset + ((DWORD64)pDataRq->ovl.u.s.OffsetHigh << 32);
1188 rtStart = MEDIATIME_FROM_BYTES(rtStart);
1189 rtStop = rtStart + MEDIATIME_FROM_BYTES(dwBytes);
1191 IMediaSample_GetTime(pDataRq->pSample, &rtSampleStart, &rtSampleStop);
1192 assert(rtStart == rtSampleStart);
1193 assert(rtStop <= rtSampleStop);
1195 IMediaSample_SetTime(pDataRq->pSample, &rtStart, &rtStop);
1196 assert(rtStart == rtSampleStart);
1198 assert(rtStop == rtSampleStop);
1200 assert(rtStop == rtStart);
1202 This->sample_list[buffer].pSample = NULL;
1203 assert(This->oldest_sample < This->samples);
1205 if (buffer == This->oldest_sample)
1208 for (x = This->oldest_sample + 1; x < This->samples; ++x)
1209 if (This->sample_list[x].pSample)
1211 if (x >= This->samples)
1212 for (x = 0; x < This->oldest_sample; ++x)
1213 if (This->sample_list[x].pSample)
1215 if (This->oldest_sample == x)
1216 /* No samples found, reset to 0 */
1218 This->oldest_sample = x;
1221 LeaveCriticalSection(&This->csList);
1223 TRACE("-- %x\n", hr);
1227 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1229 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1232 REFERENCE_TIME tStart;
1233 REFERENCE_TIME tStop;
1236 TRACE("(%p)\n", pSample);
1238 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1241 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1244 hr = FileAsyncReader_SyncRead(iface,
1245 BYTES_FROM_MEDIATIME(tStart),
1246 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1249 TRACE("-- %x\n", hr);
1253 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1257 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1259 TRACE("(%x%08x, %d, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1261 ZeroMemory(&ovl, sizeof(ovl));
1263 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1264 /* NOTE: llPosition is the actual byte position to start reading from */
1265 ovl.u.s.Offset = (DWORD) llPosition;
1266 ovl.u.s.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1268 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1269 hr = HRESULT_FROM_WIN32(GetLastError());
1271 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1278 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1279 hr = HRESULT_FROM_WIN32(GetLastError());
1282 CloseHandle(ovl.hEvent);
1284 TRACE("-- %x\n", hr);
1288 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1292 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1294 TRACE("(%p, %p)\n", pTotal, pAvailable);
1296 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1297 (GetLastError() != NO_ERROR))
1298 return HRESULT_FROM_WIN32(GetLastError());
1300 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1302 *pAvailable = *pTotal;
1307 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1309 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1313 EnterCriticalSection(&This->csList);
1314 This->bFlushing = TRUE;
1315 CancelIo(This->hFile);
1316 SetEvent(This->handle_list[This->samples]);
1317 LeaveCriticalSection(&This->csList);
1322 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1324 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1329 EnterCriticalSection(&This->csList);
1330 ResetEvent(This->handle_list[This->samples]);
1331 This->bFlushing = FALSE;
1332 for (x = 0; x < This->samples; ++x)
1333 assert(!This->sample_list[x].pSample);
1335 LeaveCriticalSection(&This->csList);
1340 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1342 FileAsyncReader_QueryInterface,
1343 FileAsyncReader_AddRef,
1344 FileAsyncReader_Release,
1345 FileAsyncReader_RequestAllocator,
1346 FileAsyncReader_Request,
1347 FileAsyncReader_WaitForNext,
1348 FileAsyncReader_SyncReadAligned,
1349 FileAsyncReader_SyncRead,
1350 FileAsyncReader_Length,
1351 FileAsyncReader_BeginFlush,
1352 FileAsyncReader_EndFlush,
1356 static AsyncReader *from_IAMFilterMiscFlags(IAMFilterMiscFlags *iface) {
1357 return (AsyncReader*)((char*)iface - offsetof(AsyncReader, IAMFilterMiscFlags_vtbl));
1360 static HRESULT WINAPI AMFilterMiscFlags_QueryInterface(IAMFilterMiscFlags *iface, REFIID riid, void **ppv) {
1361 AsyncReader *This = from_IAMFilterMiscFlags(iface);
1362 return IUnknown_QueryInterface((IUnknown*)This, riid, ppv);
1365 static ULONG WINAPI AMFilterMiscFlags_AddRef(IAMFilterMiscFlags *iface) {
1366 AsyncReader *This = from_IAMFilterMiscFlags(iface);
1367 return IUnknown_AddRef((IUnknown*)This);
1370 static ULONG WINAPI AMFilterMiscFlags_Release(IAMFilterMiscFlags *iface) {
1371 AsyncReader *This = from_IAMFilterMiscFlags(iface);
1372 return IUnknown_Release((IUnknown*)This);
1375 static ULONG WINAPI AMFilterMiscFlags_GetMiscFlags(IAMFilterMiscFlags *iface) {
1376 return AM_FILTER_MISC_FLAGS_IS_SOURCE;
1379 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl = {
1380 AMFilterMiscFlags_QueryInterface,
1381 AMFilterMiscFlags_AddRef,
1382 AMFilterMiscFlags_Release,
1383 AMFilterMiscFlags_GetMiscFlags