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 IFileSourceFilter IFileSourceFilter_iface;
44 IAMFilterMiscFlags IAMFilterMiscFlags_iface;
51 static inline AsyncReader *impl_from_BaseFilter(BaseFilter *iface)
53 return CONTAINING_RECORD(iface, AsyncReader, filter);
56 static inline AsyncReader *impl_from_IBaseFilter(IBaseFilter *iface)
58 return CONTAINING_RECORD(iface, AsyncReader, filter.IBaseFilter_iface);
61 static inline AsyncReader *impl_from_IFileSourceFilter(IFileSourceFilter *iface)
63 return CONTAINING_RECORD(iface, AsyncReader, IFileSourceFilter_iface);
66 static inline AsyncReader *impl_from_IAMFilterMiscFlags(IAMFilterMiscFlags *iface)
68 return CONTAINING_RECORD(iface, AsyncReader, IAMFilterMiscFlags_iface);
71 static const IBaseFilterVtbl AsyncReader_Vtbl;
72 static const IFileSourceFilterVtbl FileSource_Vtbl;
73 static const IAsyncReaderVtbl FileAsyncReader_Vtbl;
74 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl;
76 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
78 static const WCHAR mediatype_name[] = {
79 'M', 'e', 'd', 'i', 'a', ' ', 'T', 'y', 'p', 'e', 0 };
80 static const WCHAR subtype_name[] = {
81 'S', 'u', 'b', 't', 'y', 'p', 'e', 0 };
82 static const WCHAR source_filter_name[] = {
83 'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
85 static HRESULT process_extensions(HKEY hkeyExtensions, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType, GUID * sourceFilter)
96 /* Get the part of the name that matters */
97 extension = PathFindExtensionW(pszFileName);
98 if (*extension != '.')
101 l = RegOpenKeyExW(hkeyExtensions, extension, 0, KEY_READ, &hsub);
107 size = sizeof(keying);
108 l = RegQueryValueExW(hsub, mediatype_name, NULL, NULL, (LPBYTE)keying, &size);
110 CLSIDFromString(keying, majorType);
115 size = sizeof(keying);
117 l = RegQueryValueExW(hsub, subtype_name, NULL, NULL, (LPBYTE)keying, &size);
119 CLSIDFromString(keying, minorType);
124 size = sizeof(keying);
126 l = RegQueryValueExW(hsub, source_filter_name, NULL, NULL, (LPBYTE)keying, &size);
128 CLSIDFromString(keying, sourceFilter);
138 static unsigned char byte_from_hex_char(WCHAR wHex)
140 switch (tolowerW(wHex))
152 return (wHex - '0') & 0xf;
159 return (wHex - 'a' + 10) & 0xf;
165 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
175 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
177 /* format: "offset, bytestocompare, mask, value" */
179 ulOffset = strtolW(wszPatternString, NULL, 10);
181 if (!(wszPatternString = strchrW(wszPatternString, ',')))
184 wszPatternString++; /* skip ',' */
186 ulBytes = strtolW(wszPatternString, NULL, 10);
188 pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
189 pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
190 pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
192 /* default mask is match everything */
193 memset(pbMask, 0xFF, ulBytes);
195 if (!(wszPatternString = strchrW(wszPatternString, ',')))
200 wszPatternString++; /* skip ',' */
201 while (!isxdigitW(*wszPatternString) && (*wszPatternString != ',')) wszPatternString++;
203 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
205 if ((strpos % 2) == 1) /* odd numbered position */
206 pbMask[strpos / 2] |= byte_from_hex_char(*wszPatternString);
208 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
211 if (!(wszPatternString = strchrW(wszPatternString, ',')))
214 wszPatternString++; /* skip ',' */
219 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
222 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
224 if ((strpos % 2) == 1) /* odd numbered position */
225 pbValue[strpos / 2] |= byte_from_hex_char(*wszPatternString);
227 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
232 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
237 for (i = 0; i < ulBytes; i++)
238 if ((pbFile[i] & pbMask[i]) != pbValue[i])
245 HeapFree(GetProcessHeap(), 0, pbMask);
246 HeapFree(GetProcessHeap(), 0, pbValue);
247 HeapFree(GetProcessHeap(), 0, pbFile);
249 /* if we encountered no errors with this string, and there is a following tuple, then we
250 * have to match that as well to succeed */
251 if ((hr == S_OK) && (wszPatternString = strchrW(wszPatternString, ',')))
252 return process_pattern_string(wszPatternString + 1, pReader);
257 HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType, GUID * sourceFilter)
259 HKEY hkeyMediaType = NULL;
263 static const WCHAR wszMediaType[] = {'M','e','d','i','a',' ','T','y','p','e',0};
265 TRACE("(%p, %s, %p, %p)\n", pReader, debugstr_w(pszFileName), majorType, minorType);
268 *majorType = GUID_NULL;
270 *minorType = GUID_NULL;
272 *sourceFilter = GUID_NULL;
274 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType);
275 hr = HRESULT_FROM_WIN32(lRet);
281 for (indexMajor = 0; !bFound; indexMajor++)
284 WCHAR wszMajorKeyName[CHARS_IN_GUID];
285 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
286 static const WCHAR wszExtensions[] = {'E','x','t','e','n','s','i','o','n','s',0};
288 if (RegEnumKeyExW(hkeyMediaType, indexMajor, wszMajorKeyName, &dwKeyNameLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
290 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
292 TRACE("%s\n", debugstr_w(wszMajorKeyName));
293 if (!strcmpW(wszExtensions, wszMajorKeyName))
295 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType, sourceFilter) == S_OK)
298 /* We need a reader interface to check bytes */
303 for (indexMinor = 0; !bFound; indexMinor++)
306 WCHAR wszMinorKeyName[CHARS_IN_GUID];
307 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
308 WCHAR wszSourceFilterKeyName[CHARS_IN_GUID];
309 DWORD dwSourceFilterKeyNameLen = sizeof(wszSourceFilterKeyName);
313 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
316 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
319 TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
321 if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
324 for (indexValue = 0; !bFound; indexValue++)
327 WCHAR wszValueName[14]; /* longest name we should encounter will be "Source Filter" */
328 LPWSTR wszPatternString = HeapAlloc(GetProcessHeap(), 0, maxValueLen);
329 DWORD dwValueNameLen = sizeof(wszValueName) / sizeof(wszValueName[0]); /* remember this is in chars */
330 DWORD dwDataLen = maxValueLen; /* remember this is in bytes */
332 if (RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen) != ERROR_SUCCESS)
334 HeapFree(GetProcessHeap(), 0, wszPatternString);
338 if (strcmpW(wszValueName, source_filter_name)==0)
341 /* if it is not the source filter value */
342 if (process_pattern_string(wszPatternString, pReader) == S_OK)
344 if (majorType && FAILED(CLSIDFromString(wszMajorKeyName, majorType)))
346 if (minorType && FAILED(CLSIDFromString(wszMinorKeyName, minorType)))
350 /* Look up the source filter key */
351 if (RegQueryValueExW(hkeyMinor, source_filter_name, NULL, NULL, (LPBYTE)wszSourceFilterKeyName, &dwSourceFilterKeyNameLen))
353 if (FAILED(CLSIDFromString(wszSourceFilterKeyName, sourceFilter)))
358 HeapFree(GetProcessHeap(), 0, wszPatternString);
360 CloseHandle(hkeyMinor);
363 CloseHandle(hkeyMajor);
366 CloseHandle(hkeyMediaType);
368 if (SUCCEEDED(hr) && !bFound)
370 ERR("Media class not found\n");
375 TRACE("Found file's class:\n");
377 TRACE("\tmajor = %s\n", qzdebugstr_guid(majorType));
379 TRACE("\tsubtype = %s\n", qzdebugstr_guid(minorType));
381 TRACE("\tsource filter = %s\n", qzdebugstr_guid(sourceFilter));
387 static IPin* WINAPI AsyncReader_GetPin(BaseFilter *iface, int pos)
389 AsyncReader *This = impl_from_BaseFilter(iface);
391 if (pos >= 1 || !This->pOutputPin)
394 IPin_AddRef(This->pOutputPin);
395 return This->pOutputPin;
398 static LONG WINAPI AsyncReader_GetPinCount(BaseFilter *iface)
400 AsyncReader *This = impl_from_BaseFilter(iface);
402 if (!This->pOutputPin)
408 static const BaseFilterFuncTable BaseFuncTable = {
410 AsyncReader_GetPinCount
413 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
415 AsyncReader *pAsyncRead;
418 return CLASS_E_NOAGGREGATION;
420 pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
423 return E_OUTOFMEMORY;
425 BaseFilter_Init(&pAsyncRead->filter, &AsyncReader_Vtbl, &CLSID_AsyncReader, (DWORD_PTR)(__FILE__ ": AsyncReader.csFilter"), &BaseFuncTable);
427 pAsyncRead->IFileSourceFilter_iface.lpVtbl = &FileSource_Vtbl;
428 pAsyncRead->IAMFilterMiscFlags_iface.lpVtbl = &IAMFilterMiscFlags_Vtbl;
429 pAsyncRead->pOutputPin = NULL;
431 pAsyncRead->pszFileName = NULL;
432 pAsyncRead->pmt = NULL;
436 TRACE("-- created at %p\n", pAsyncRead);
441 /** IUnknown methods **/
443 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
445 AsyncReader *This = impl_from_IBaseFilter(iface);
447 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
451 if (IsEqualIID(riid, &IID_IUnknown))
453 else if (IsEqualIID(riid, &IID_IPersist))
455 else if (IsEqualIID(riid, &IID_IMediaFilter))
457 else if (IsEqualIID(riid, &IID_IBaseFilter))
459 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
460 *ppv = &This->IFileSourceFilter_iface;
461 else if (IsEqualIID(riid, &IID_IAMFilterMiscFlags))
462 *ppv = &This->IAMFilterMiscFlags_iface;
466 IUnknown_AddRef((IUnknown *)(*ppv));
470 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking) &&
471 !IsEqualIID(riid, &IID_IVideoWindow) && !IsEqualIID(riid, &IID_IBasicAudio))
472 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
474 return E_NOINTERFACE;
477 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
479 AsyncReader *This = impl_from_IBaseFilter(iface);
480 ULONG refCount = BaseFilterImpl_Release(iface);
482 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
486 if (This->pOutputPin)
489 if(SUCCEEDED(IPin_ConnectedTo(This->pOutputPin, &pConnectedTo)))
491 IPin_Disconnect(pConnectedTo);
492 IPin_Release(pConnectedTo);
494 IPin_Disconnect(This->pOutputPin);
495 IPin_Release(This->pOutputPin);
497 CoTaskMemFree(This->pszFileName);
499 FreeMediaType(This->pmt);
507 /** IMediaFilter methods **/
509 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
511 AsyncReader *This = impl_from_IBaseFilter(iface);
515 This->filter.state = State_Stopped;
520 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
522 AsyncReader *This = impl_from_IBaseFilter(iface);
526 This->filter.state = State_Paused;
531 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
533 AsyncReader *This = impl_from_IBaseFilter(iface);
535 TRACE("(%x%08x)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
537 This->filter.state = State_Running;
542 /** IBaseFilter methods **/
544 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
546 AsyncReader *This = impl_from_IBaseFilter(iface);
547 TRACE("(%s, %p)\n", debugstr_w(Id), ppPin);
552 if (strcmpW(Id, wszOutputPinName))
555 return VFW_E_NOT_FOUND;
558 *ppPin = This->pOutputPin;
563 static const IBaseFilterVtbl AsyncReader_Vtbl =
565 AsyncReader_QueryInterface,
566 BaseFilterImpl_AddRef,
568 BaseFilterImpl_GetClassID,
572 BaseFilterImpl_GetState,
573 BaseFilterImpl_SetSyncSource,
574 BaseFilterImpl_GetSyncSource,
575 BaseFilterImpl_EnumPins,
577 BaseFilterImpl_QueryFilterInfo,
578 BaseFilterImpl_JoinFilterGraph,
579 BaseFilterImpl_QueryVendorInfo
582 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
584 AsyncReader *This = impl_from_IFileSourceFilter(iface);
586 return IBaseFilter_QueryInterface(&This->filter.IBaseFilter_iface, riid, ppv);
589 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
591 AsyncReader *This = impl_from_IFileSourceFilter(iface);
593 return IBaseFilter_AddRef(&This->filter.IBaseFilter_iface);
596 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
598 AsyncReader *This = impl_from_IFileSourceFilter(iface);
600 return IBaseFilter_Release(&This->filter.IBaseFilter_iface);
603 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
607 IAsyncReader * pReader = NULL;
608 AsyncReader *This = impl_from_IFileSourceFilter(iface);
610 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
613 /* FIXME: check the sharing values that native uses */
614 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
616 if (hFile == INVALID_HANDLE_VALUE)
618 return HRESULT_FROM_WIN32(GetLastError());
622 hr = FileAsyncReader_Construct(hFile, &This->filter.IBaseFilter_iface, &This->filter.csFilter, &This->pOutputPin);
623 BaseFilterImpl_IncrementPinVersion(&This->filter);
626 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
628 /* store file name & media type */
631 CoTaskMemFree(This->pszFileName);
633 FreeMediaType(This->pmt);
635 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
636 strcpyW(This->pszFileName, pszFileName);
638 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
641 This->pmt->bFixedSizeSamples = TRUE;
642 This->pmt->bTemporalCompression = FALSE;
643 This->pmt->cbFormat = 0;
644 This->pmt->pbFormat = NULL;
645 This->pmt->pUnk = NULL;
646 This->pmt->lSampleSize = 0;
647 This->pmt->formattype = FORMAT_None;
648 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype, NULL);
651 CoTaskMemFree(This->pmt);
656 CopyMediaType(This->pmt, pmt);
660 IAsyncReader_Release(pReader);
664 if (This->pOutputPin)
666 IPin_Release(This->pOutputPin);
667 This->pOutputPin = NULL;
670 CoTaskMemFree(This->pszFileName);
672 FreeMediaType(This->pmt);
673 This->pszFileName = NULL;
679 /* FIXME: check return codes */
683 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
685 AsyncReader *This = impl_from_IFileSourceFilter(iface);
687 TRACE("(%p, %p)\n", ppszFileName, pmt);
692 /* copy file name & media type if available, otherwise clear the outputs */
693 if (This->pszFileName)
695 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
696 strcpyW(*ppszFileName, This->pszFileName);
699 *ppszFileName = NULL;
704 CopyMediaType(pmt, This->pmt);
706 ZeroMemory(pmt, sizeof(*pmt));
712 static const IFileSourceFilterVtbl FileSource_Vtbl =
714 FileSource_QueryInterface,
718 FileSource_GetCurFile
722 /* the dwUserData passed back to user */
723 typedef struct DATAREQUEST
725 IMediaSample * pSample; /* sample passed to us by user */
726 DWORD_PTR dwUserData; /* user data passed to us */
727 OVERLAPPED ovl; /* our overlapped structure */
730 typedef struct FileAsyncReader
733 IAsyncReader IAsyncReader_iface;
735 ALLOCATOR_PROPERTIES allocProps;
738 /* Why would you need more? Every sample has its own handle */
742 CRITICAL_SECTION csList; /* critical section to prevent concurrency issues */
743 DATAREQUEST *sample_list;
745 /* Have a handle for every sample, and then one more as flushing handle */
749 static inline FileAsyncReader *impl_from_IPin(IPin *iface)
751 return CONTAINING_RECORD(iface, FileAsyncReader, pin.pin.IPin_iface);
754 static inline FileAsyncReader *impl_from_BasePin(BasePin *iface)
756 return CONTAINING_RECORD(iface, FileAsyncReader, pin.pin);
759 static inline FileAsyncReader *impl_from_BaseOutputPin(BaseOutputPin *iface)
761 return CONTAINING_RECORD(iface, FileAsyncReader, pin);
764 static inline BaseOutputPin *impl_BaseOututPin_from_BasePin(BasePin *iface)
766 return CONTAINING_RECORD(iface, BaseOutputPin, pin);
769 static inline FileAsyncReader *impl_from_IAsyncReader(IAsyncReader *iface)
771 return CONTAINING_RECORD(iface, FileAsyncReader, IAsyncReader_iface);
774 static HRESULT WINAPI FileAsyncReaderPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *pmt)
776 FileAsyncReader *This = impl_from_IPin(iface);
777 AM_MEDIA_TYPE *pmt_filter = impl_from_IBaseFilter(This->pin.pin.pinInfo.pFilter)->pmt;
779 FIXME("(%p, %p)\n", iface, pmt);
781 if (IsEqualGUID(&pmt->majortype, &pmt_filter->majortype) &&
782 IsEqualGUID(&pmt->subtype, &pmt_filter->subtype) &&
783 IsEqualGUID(&pmt->formattype, &FORMAT_None))
789 static HRESULT WINAPI FileAsyncReaderPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
791 FileAsyncReader *This = impl_from_BasePin(iface);
795 return VFW_S_NO_MORE_ITEMS;
796 CopyMediaType(pmt, impl_from_IBaseFilter(This->pin.pin.pinInfo.pFilter)->pmt);
800 /* overridden pin functions */
802 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
804 FileAsyncReader *This = impl_from_IPin(iface);
805 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
809 if (IsEqualIID(riid, &IID_IUnknown))
811 else if (IsEqualIID(riid, &IID_IPin))
813 else if (IsEqualIID(riid, &IID_IAsyncReader))
814 *ppv = &This->IAsyncReader_iface;
818 IUnknown_AddRef((IUnknown *)(*ppv));
822 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking))
823 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
825 return E_NOINTERFACE;
828 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
830 FileAsyncReader *This = impl_from_IPin(iface);
831 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
834 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
838 CoTaskMemFree(This->sample_list);
839 if (This->handle_list)
841 for (x = 0; x <= This->samples; ++x)
842 CloseHandle(This->handle_list[x]);
843 CoTaskMemFree(This->handle_list);
845 CloseHandle(This->hFile);
846 This->csList.DebugInfo->Spare[0] = 0;
847 DeleteCriticalSection(&This->csList);
854 static const IPinVtbl FileAsyncReaderPin_Vtbl =
856 FileAsyncReaderPin_QueryInterface,
858 FileAsyncReaderPin_Release,
859 BaseOutputPinImpl_Connect,
860 BaseOutputPinImpl_ReceiveConnection,
861 BasePinImpl_Disconnect,
862 BasePinImpl_ConnectedTo,
863 BasePinImpl_ConnectionMediaType,
864 BasePinImpl_QueryPinInfo,
865 BasePinImpl_QueryDirection,
867 FileAsyncReaderPin_QueryAccept,
868 BasePinImpl_EnumMediaTypes,
869 BasePinImpl_QueryInternalConnections,
870 BaseOutputPinImpl_EndOfStream,
871 BaseOutputPinImpl_BeginFlush,
872 BaseOutputPinImpl_EndFlush,
873 BasePinImpl_NewSegment
876 /* Function called as a helper to IPin_Connect */
877 /* specific AM_MEDIA_TYPE - it cannot be NULL */
878 /* this differs from standard OutputPin_AttemptConnection only in that it
879 * doesn't need the IMemInputPin interface on the receiving pin */
880 static HRESULT WINAPI FileAsyncReaderPin_AttemptConnection(BasePin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
882 BaseOutputPin *This = impl_BaseOututPin_from_BasePin(iface);
885 TRACE("(%p, %p)\n", pReceivePin, pmt);
886 dump_AM_MEDIA_TYPE(pmt);
888 /* FIXME: call queryacceptproc */
890 This->pin.pConnectedTo = pReceivePin;
891 IPin_AddRef(pReceivePin);
892 CopyMediaType(&This->pin.mtCurrent, pmt);
894 hr = IPin_ReceiveConnection(pReceivePin, &iface->IPin_iface, pmt);
898 IPin_Release(This->pin.pConnectedTo);
899 This->pin.pConnectedTo = NULL;
900 FreeMediaType(&This->pin.mtCurrent);
903 TRACE(" -- %x\n", hr);
907 static HRESULT WINAPI FileAsyncReaderPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
909 FileAsyncReader *This = impl_from_BaseOutputPin(iface);
910 ALLOCATOR_PROPERTIES actual;
912 if (ppropInputRequest->cbAlign && ppropInputRequest->cbAlign != This->allocProps.cbAlign)
913 FIXME("Requested Buffer cbAlign mismatch %i,%i\n",This->allocProps.cbAlign, ppropInputRequest->cbAlign);
914 if (ppropInputRequest->cbPrefix)
915 FIXME("Requested Buffer cbPrefix mismatch %i,%i\n",This->allocProps.cbPrefix, ppropInputRequest->cbPrefix);
916 if (ppropInputRequest->cbBuffer)
917 FIXME("Requested Buffer cbBuffer mismatch %i,%i\n",This->allocProps.cbBuffer, ppropInputRequest->cbBuffer);
918 if (ppropInputRequest->cBuffers)
919 FIXME("Requested Buffer cBuffers mismatch %i,%i\n",This->allocProps.cBuffers, ppropInputRequest->cBuffers);
921 return IMemAllocator_SetProperties(pAlloc, &This->allocProps, &actual);
924 static const BasePinFuncTable output_BaseFuncTable = {
926 FileAsyncReaderPin_AttemptConnection,
927 BasePinImpl_GetMediaTypeVersion,
928 FileAsyncReaderPin_GetMediaType
931 static const BaseOutputPinFuncTable output_BaseOutputFuncTable = {
932 FileAsyncReaderPin_DecideBufferSize,
933 BaseOutputPinImpl_DecideAllocator,
934 BaseOutputPinImpl_BreakConnect
937 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
943 piOutput.dir = PINDIR_OUTPUT;
944 piOutput.pFilter = pBaseFilter;
945 strcpyW(piOutput.achName, wszOutputPinName);
946 hr = BaseOutputPin_Construct(&FileAsyncReaderPin_Vtbl, sizeof(FileAsyncReader), &piOutput, &output_BaseFuncTable, &output_BaseOutputFuncTable, pCritSec, ppPin);
950 FileAsyncReader *pPinImpl = (FileAsyncReader *)*ppPin;
951 pPinImpl->IAsyncReader_iface.lpVtbl = &FileAsyncReader_Vtbl;
952 pPinImpl->hFile = hFile;
953 pPinImpl->bFlushing = FALSE;
954 pPinImpl->sample_list = NULL;
955 pPinImpl->handle_list = NULL;
956 pPinImpl->queued_number = 0;
957 InitializeCriticalSection(&pPinImpl->csList);
958 pPinImpl->csList.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FileAsyncReader.csList");
965 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
967 FileAsyncReader *This = impl_from_IAsyncReader(iface);
969 return IPin_QueryInterface(&This->pin.pin.IPin_iface, riid, ppv);
972 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
974 FileAsyncReader *This = impl_from_IAsyncReader(iface);
976 return IPin_AddRef(&This->pin.pin.IPin_iface);
979 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
981 FileAsyncReader *This = impl_from_IAsyncReader(iface);
983 return IPin_Release(&This->pin.pin.IPin_iface);
986 #define DEF_ALIGNMENT 1
988 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
990 FileAsyncReader *This = impl_from_IAsyncReader(iface);
994 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
996 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
997 pProps->cbAlign = DEF_ALIGNMENT;
1001 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
1002 /* FIXME: check we are still aligned */
1005 IMemAllocator_AddRef(pPreferred);
1006 *ppActual = pPreferred;
1007 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
1014 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
1018 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
1019 /* FIXME: check we are still aligned */
1022 *ppActual = pPreferred;
1023 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
1030 CoTaskMemFree(This->sample_list);
1031 if (This->handle_list)
1034 for (x = 0; x <= This->samples; ++x)
1035 CloseHandle(This->handle_list[x]);
1036 CoTaskMemFree(This->handle_list);
1039 This->samples = pProps->cBuffers;
1040 This->oldest_sample = 0;
1041 TRACE("Samples: %u\n", This->samples);
1042 This->sample_list = CoTaskMemAlloc(sizeof(This->sample_list[0]) * pProps->cBuffers);
1043 This->handle_list = CoTaskMemAlloc(sizeof(HANDLE) * pProps->cBuffers * 2);
1045 if (This->sample_list && This->handle_list)
1048 ZeroMemory(This->sample_list, sizeof(This->sample_list[0]) * pProps->cBuffers);
1049 for (x = 0; x < This->samples; ++x)
1051 This->sample_list[x].ovl.hEvent = This->handle_list[x] = CreateEventW(NULL, 0, 0, NULL);
1052 if (x + 1 < This->samples)
1053 This->handle_list[This->samples + 1 + x] = This->handle_list[x];
1055 This->handle_list[This->samples] = CreateEventW(NULL, 1, 0, NULL);
1056 This->allocProps = *pProps;
1061 CoTaskMemFree(This->sample_list);
1062 CoTaskMemFree(This->handle_list);
1064 This->sample_list = NULL;
1065 This->handle_list = NULL;
1073 IMemAllocator_Release(pPreferred);
1076 TRACE("-- %x\n", hr);
1080 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
1081 * however, this would be quite complicated to do and may be a bit error prone */
1082 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
1085 REFERENCE_TIME Start;
1086 REFERENCE_TIME Stop;
1087 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1088 LPBYTE pBuffer = NULL;
1090 TRACE("(%p, %lx)\n", pSample, dwUser);
1095 /* get start and stop positions in bytes */
1097 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
1100 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1102 EnterCriticalSection(&This->csList);
1103 if (This->bFlushing)
1105 LeaveCriticalSection(&This->csList);
1106 return VFW_E_WRONG_STATE;
1111 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
1112 DATAREQUEST *pDataRq;
1115 /* Try to insert above the waiting sample if possible */
1116 for (x = This->oldest_sample; x < This->samples; ++x)
1118 if (!This->sample_list[x].pSample)
1122 if (x >= This->samples)
1123 for (x = 0; x < This->oldest_sample; ++x)
1125 if (!This->sample_list[x].pSample)
1129 /* There must be a sample we have found */
1130 assert(x < This->samples);
1131 ++This->queued_number;
1133 pDataRq = This->sample_list + x;
1135 pDataRq->ovl.u.s.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
1136 pDataRq->ovl.u.s.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
1137 pDataRq->dwUserData = dwUser;
1139 /* we violate traditional COM rules here by maintaining
1140 * a reference to the sample, but not calling AddRef, but
1141 * that's what MSDN says to do */
1142 pDataRq->pSample = pSample;
1144 /* this is definitely not how it is implemented on Win9x
1145 * as they do not support async reads on files, but it is
1146 * sooo much easier to use this than messing around with threads!
1148 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1149 hr = HRESULT_FROM_WIN32(GetLastError());
1151 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1152 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1156 LeaveCriticalSection(&This->csList);
1158 TRACE("-- %x\n", hr);
1162 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1165 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1168 TRACE("(%u, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1173 EnterCriticalSection(&This->csList);
1174 if (!This->bFlushing)
1176 LONG oldest = This->oldest_sample;
1178 if (!This->queued_number)
1180 /* It could be that nothing is queued right now, but that can be fixed */
1181 WARN("Called without samples in queue and not flushing!!\n");
1183 LeaveCriticalSection(&This->csList);
1185 /* wait for an object to read, or time out */
1186 buffer = WaitForMultipleObjectsEx(This->samples+1, This->handle_list + oldest, FALSE, dwTimeout, TRUE);
1188 EnterCriticalSection(&This->csList);
1189 if (buffer <= This->samples)
1191 /* Re-scale the buffer back to normal */
1194 /* Uh oh, we overshot the flusher handle, renormalize it back to 0..Samples-1 */
1195 if (buffer > This->samples)
1196 buffer -= This->samples + 1;
1197 assert(buffer <= This->samples);
1200 if (buffer >= This->samples)
1202 if (buffer != This->samples)
1204 FIXME("Returned: %u (%08x)\n", buffer, GetLastError());
1208 hr = VFW_E_WRONG_STATE;
1212 --This->queued_number;
1215 if (This->bFlushing && buffer == ~0)
1217 for (buffer = 0; buffer < This->samples; ++buffer)
1219 if (This->sample_list[buffer].pSample)
1221 ResetEvent(This->handle_list[buffer]);
1225 if (buffer == This->samples)
1227 assert(!This->queued_number);
1232 --This->queued_number;
1239 REFERENCE_TIME rtStart, rtStop;
1240 REFERENCE_TIME rtSampleStart, rtSampleStop;
1241 DATAREQUEST *pDataRq = This->sample_list + buffer;
1244 /* get any errors */
1245 if (!This->bFlushing && !GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1246 hr = HRESULT_FROM_WIN32(GetLastError());
1248 /* Return the sample no matter what so it can be destroyed */
1249 *ppSample = pDataRq->pSample;
1250 *pdwUser = pDataRq->dwUserData;
1252 if (This->bFlushing)
1253 hr = VFW_E_WRONG_STATE;
1258 /* Set the time on the sample */
1259 IMediaSample_SetActualDataLength(pDataRq->pSample, dwBytes);
1261 rtStart = (DWORD64)pDataRq->ovl.u.s.Offset + ((DWORD64)pDataRq->ovl.u.s.OffsetHigh << 32);
1262 rtStart = MEDIATIME_FROM_BYTES(rtStart);
1263 rtStop = rtStart + MEDIATIME_FROM_BYTES(dwBytes);
1265 IMediaSample_GetTime(pDataRq->pSample, &rtSampleStart, &rtSampleStop);
1266 assert(rtStart == rtSampleStart);
1267 assert(rtStop <= rtSampleStop);
1269 IMediaSample_SetTime(pDataRq->pSample, &rtStart, &rtStop);
1270 assert(rtStart == rtSampleStart);
1272 assert(rtStop == rtSampleStop);
1274 assert(rtStop == rtStart);
1276 This->sample_list[buffer].pSample = NULL;
1277 assert(This->oldest_sample < This->samples);
1279 if (buffer == This->oldest_sample)
1282 for (x = This->oldest_sample + 1; x < This->samples; ++x)
1283 if (This->sample_list[x].pSample)
1285 if (x >= This->samples)
1286 for (x = 0; x < This->oldest_sample; ++x)
1287 if (This->sample_list[x].pSample)
1289 if (This->oldest_sample == x)
1290 /* No samples found, reset to 0 */
1292 This->oldest_sample = x;
1295 LeaveCriticalSection(&This->csList);
1297 TRACE("-- %x\n", hr);
1301 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1303 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1306 REFERENCE_TIME tStart;
1307 REFERENCE_TIME tStop;
1310 TRACE("(%p)\n", pSample);
1312 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1315 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1318 hr = FileAsyncReader_SyncRead(iface,
1319 BYTES_FROM_MEDIATIME(tStart),
1320 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1323 TRACE("-- %x\n", hr);
1327 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1331 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1333 TRACE("(%x%08x, %d, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1335 ZeroMemory(&ovl, sizeof(ovl));
1337 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1338 /* NOTE: llPosition is the actual byte position to start reading from */
1339 ovl.u.s.Offset = (DWORD) llPosition;
1340 ovl.u.s.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1342 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1343 hr = HRESULT_FROM_WIN32(GetLastError());
1345 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1352 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1353 hr = HRESULT_FROM_WIN32(GetLastError());
1356 CloseHandle(ovl.hEvent);
1358 TRACE("-- %x\n", hr);
1362 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1366 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1368 TRACE("(%p, %p)\n", pTotal, pAvailable);
1370 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1371 (GetLastError() != NO_ERROR))
1372 return HRESULT_FROM_WIN32(GetLastError());
1374 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1376 *pAvailable = *pTotal;
1381 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1383 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1387 EnterCriticalSection(&This->csList);
1388 This->bFlushing = TRUE;
1389 CancelIo(This->hFile);
1390 SetEvent(This->handle_list[This->samples]);
1391 LeaveCriticalSection(&This->csList);
1396 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1398 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1403 EnterCriticalSection(&This->csList);
1404 ResetEvent(This->handle_list[This->samples]);
1405 This->bFlushing = FALSE;
1406 for (x = 0; x < This->samples; ++x)
1407 assert(!This->sample_list[x].pSample);
1409 LeaveCriticalSection(&This->csList);
1414 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1416 FileAsyncReader_QueryInterface,
1417 FileAsyncReader_AddRef,
1418 FileAsyncReader_Release,
1419 FileAsyncReader_RequestAllocator,
1420 FileAsyncReader_Request,
1421 FileAsyncReader_WaitForNext,
1422 FileAsyncReader_SyncReadAligned,
1423 FileAsyncReader_SyncRead,
1424 FileAsyncReader_Length,
1425 FileAsyncReader_BeginFlush,
1426 FileAsyncReader_EndFlush,
1430 static HRESULT WINAPI AMFilterMiscFlags_QueryInterface(IAMFilterMiscFlags *iface, REFIID riid, void **ppv) {
1431 AsyncReader *This = impl_from_IAMFilterMiscFlags(iface);
1432 return IUnknown_QueryInterface((IUnknown*)This, riid, ppv);
1435 static ULONG WINAPI AMFilterMiscFlags_AddRef(IAMFilterMiscFlags *iface) {
1436 AsyncReader *This = impl_from_IAMFilterMiscFlags(iface);
1437 return IUnknown_AddRef((IUnknown*)This);
1440 static ULONG WINAPI AMFilterMiscFlags_Release(IAMFilterMiscFlags *iface) {
1441 AsyncReader *This = impl_from_IAMFilterMiscFlags(iface);
1442 return IUnknown_Release((IUnknown*)This);
1445 static ULONG WINAPI AMFilterMiscFlags_GetMiscFlags(IAMFilterMiscFlags *iface) {
1446 return AM_FILTER_MISC_FLAGS_IS_SOURCE;
1449 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl = {
1450 AMFilterMiscFlags_QueryInterface,
1451 AMFilterMiscFlags_AddRef,
1452 AMFilterMiscFlags_Release,
1453 AMFilterMiscFlags_GetMiscFlags