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 };
83 static HRESULT
process_extensions(HKEY hkeyExtensions
, LPCOLESTR pszFileName
, GUID
* majorType
, GUID
* minorType
)
94 /* Get the part of the name that matters */
95 extension
= PathFindExtensionW(pszFileName
);
96 if (*extension
!= '.')
99 l
= RegOpenKeyExW(hkeyExtensions
, extension
, 0, KEY_READ
, &hsub
);
103 size
= sizeof(keying
);
104 l
= RegQueryValueExW(hsub
, mediatype_name
, NULL
, NULL
, (LPBYTE
)keying
, &size
);
106 CLSIDFromString(keying
, majorType
);
108 size
= sizeof(keying
);
110 l
= RegQueryValueExW(hsub
, subtype_name
, NULL
, NULL
, (LPBYTE
)keying
, &size
);
113 CLSIDFromString(keying
, minorType
);
122 static unsigned char byte_from_hex_char(WCHAR wHex
)
124 switch (tolowerW(wHex
))
136 return (wHex
- '0') & 0xf;
143 return (wHex
- 'a' + 10) & 0xf;
149 static HRESULT
process_pattern_string(LPCWSTR wszPatternString
, IAsyncReader
* pReader
)
159 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString
));
161 /* format: "offset, bytestocompare, mask, value" */
163 ulOffset
= strtolW(wszPatternString
, NULL
, 10);
165 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
168 wszPatternString
++; /* skip ',' */
170 ulBytes
= strtolW(wszPatternString
, NULL
, 10);
172 pbMask
= HeapAlloc(GetProcessHeap(), 0, ulBytes
);
173 pbValue
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, ulBytes
);
174 pbFile
= HeapAlloc(GetProcessHeap(), 0, ulBytes
);
176 /* default mask is match everything */
177 memset(pbMask
, 0xFF, ulBytes
);
179 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
184 wszPatternString
++; /* skip ',' */
185 while (!isxdigitW(*wszPatternString
) && (*wszPatternString
!= ',')) wszPatternString
++;
187 for (strpos
= 0; isxdigitW(*wszPatternString
) && (strpos
/2 < ulBytes
); wszPatternString
++, strpos
++)
189 if ((strpos
% 2) == 1) /* odd numbered position */
190 pbMask
[strpos
/ 2] |= byte_from_hex_char(*wszPatternString
);
192 pbMask
[strpos
/ 2] = byte_from_hex_char(*wszPatternString
) << 4;
195 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
198 wszPatternString
++; /* skip ',' */
203 for ( ; !isxdigitW(*wszPatternString
) && (*wszPatternString
!= ','); wszPatternString
++)
206 for (strpos
= 0; isxdigitW(*wszPatternString
) && (strpos
/2 < ulBytes
); wszPatternString
++, strpos
++)
208 if ((strpos
% 2) == 1) /* odd numbered position */
209 pbValue
[strpos
/ 2] |= byte_from_hex_char(*wszPatternString
);
211 pbValue
[strpos
/ 2] = byte_from_hex_char(*wszPatternString
) << 4;
216 hr
= IAsyncReader_SyncRead(pReader
, ulOffset
, ulBytes
, pbFile
);
221 for (i
= 0; i
< ulBytes
; i
++)
222 if ((pbFile
[i
] & pbMask
[i
]) != pbValue
[i
])
229 HeapFree(GetProcessHeap(), 0, pbMask
);
230 HeapFree(GetProcessHeap(), 0, pbValue
);
231 HeapFree(GetProcessHeap(), 0, pbFile
);
233 /* if we encountered no errors with this string, and there is a following tuple, then we
234 * have to match that as well to succeed */
235 if ((hr
== S_OK
) && (wszPatternString
= strchrW(wszPatternString
, ',')))
236 return process_pattern_string(wszPatternString
+ 1, pReader
);
241 static HRESULT
GetClassMediaFile(IAsyncReader
* pReader
, LPCOLESTR pszFileName
, GUID
* majorType
, GUID
* minorType
)
243 HKEY hkeyMediaType
= NULL
;
247 static const WCHAR wszMediaType
[] = {'M','e','d','i','a',' ','T','y','p','e',0};
249 TRACE("(%p, %s, %p, %p)\n", pReader
, debugstr_w(pszFileName
), majorType
, minorType
);
251 *majorType
= GUID_NULL
;
252 *minorType
= GUID_NULL
;
254 lRet
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, wszMediaType
, 0, KEY_READ
, &hkeyMediaType
);
255 hr
= HRESULT_FROM_WIN32(lRet
);
261 for (indexMajor
= 0; !bFound
; indexMajor
++)
264 WCHAR wszMajorKeyName
[CHARS_IN_GUID
];
265 DWORD dwKeyNameLength
= sizeof(wszMajorKeyName
) / sizeof(wszMajorKeyName
[0]);
266 static const WCHAR wszExtensions
[] = {'E','x','t','e','n','s','i','o','n','s',0};
268 if (RegEnumKeyExW(hkeyMediaType
, indexMajor
, wszMajorKeyName
, &dwKeyNameLength
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
270 if (RegOpenKeyExW(hkeyMediaType
, wszMajorKeyName
, 0, KEY_READ
, &hkeyMajor
) != ERROR_SUCCESS
)
272 TRACE("%s\n", debugstr_w(wszMajorKeyName
));
273 if (!strcmpW(wszExtensions
, wszMajorKeyName
))
275 if (process_extensions(hkeyMajor
, pszFileName
, majorType
, minorType
) == S_OK
)
282 for (indexMinor
= 0; !bFound
; indexMinor
++)
285 WCHAR wszMinorKeyName
[CHARS_IN_GUID
];
286 DWORD dwMinorKeyNameLen
= sizeof(wszMinorKeyName
) / sizeof(wszMinorKeyName
[0]);
290 if (RegEnumKeyExW(hkeyMajor
, indexMinor
, wszMinorKeyName
, &dwMinorKeyNameLen
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
293 if (RegOpenKeyExW(hkeyMajor
, wszMinorKeyName
, 0, KEY_READ
, &hkeyMinor
) != ERROR_SUCCESS
)
296 TRACE("\t%s\n", debugstr_w(wszMinorKeyName
));
298 if (RegQueryInfoKeyW(hkeyMinor
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &maxValueLen
, NULL
, NULL
) != ERROR_SUCCESS
)
301 for (indexValue
= 0; !bFound
; indexValue
++)
304 WCHAR wszValueName
[14]; /* longest name we should encounter will be "Source Filter" */
305 LPWSTR wszPatternString
= HeapAlloc(GetProcessHeap(), 0, maxValueLen
);
306 DWORD dwValueNameLen
= sizeof(wszValueName
) / sizeof(wszValueName
[0]); /* remember this is in chars */
307 DWORD dwDataLen
= maxValueLen
; /* remember this is in bytes */
308 static const WCHAR wszSourceFilter
[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
310 if (RegEnumValueW(hkeyMinor
, indexValue
, wszValueName
, &dwValueNameLen
, NULL
, &dwType
, (LPBYTE
)wszPatternString
, &dwDataLen
) != ERROR_SUCCESS
)
312 HeapFree(GetProcessHeap(), 0, wszPatternString
);
316 /* if it is not the source filter value */
317 if (strcmpW(wszValueName
, wszSourceFilter
))
319 if (process_pattern_string(wszPatternString
, pReader
) == S_OK
)
321 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName
, majorType
)) &&
322 SUCCEEDED(CLSIDFromString(wszMinorKeyName
, minorType
)))
326 HeapFree(GetProcessHeap(), 0, wszPatternString
);
328 CloseHandle(hkeyMinor
);
331 CloseHandle(hkeyMajor
);
334 CloseHandle(hkeyMediaType
);
336 if (SUCCEEDED(hr
) && !bFound
)
338 ERR("Media class not found\n");
342 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType
), qzdebugstr_guid(minorType
));
347 static IPin
* WINAPI
AsyncReader_GetPin(BaseFilter
*iface
, int pos
)
349 AsyncReader
*This
= impl_from_BaseFilter(iface
);
351 if (pos
>= 1 || !This
->pOutputPin
)
354 IPin_AddRef(This
->pOutputPin
);
355 return This
->pOutputPin
;
358 static LONG WINAPI
AsyncReader_GetPinCount(BaseFilter
*iface
)
360 AsyncReader
*This
= impl_from_BaseFilter(iface
);
362 if (!This
->pOutputPin
)
368 static const BaseFilterFuncTable BaseFuncTable
= {
370 AsyncReader_GetPinCount
373 HRESULT
AsyncReader_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
375 AsyncReader
*pAsyncRead
;
378 return CLASS_E_NOAGGREGATION
;
380 pAsyncRead
= CoTaskMemAlloc(sizeof(AsyncReader
));
383 return E_OUTOFMEMORY
;
385 BaseFilter_Init(&pAsyncRead
->filter
, &AsyncReader_Vtbl
, &CLSID_AsyncReader
, (DWORD_PTR
)(__FILE__
": AsyncReader.csFilter"), &BaseFuncTable
);
387 pAsyncRead
->IFileSourceFilter_iface
.lpVtbl
= &FileSource_Vtbl
;
388 pAsyncRead
->IAMFilterMiscFlags_iface
.lpVtbl
= &IAMFilterMiscFlags_Vtbl
;
389 pAsyncRead
->pOutputPin
= NULL
;
391 pAsyncRead
->pszFileName
= NULL
;
392 pAsyncRead
->pmt
= NULL
;
396 TRACE("-- created at %p\n", pAsyncRead
);
401 /** IUnknown methods **/
403 static HRESULT WINAPI
AsyncReader_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
405 AsyncReader
*This
= impl_from_IBaseFilter(iface
);
407 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
411 if (IsEqualIID(riid
, &IID_IUnknown
))
413 else if (IsEqualIID(riid
, &IID_IPersist
))
415 else if (IsEqualIID(riid
, &IID_IMediaFilter
))
417 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
419 else if (IsEqualIID(riid
, &IID_IFileSourceFilter
))
420 *ppv
= &This
->IFileSourceFilter_iface
;
421 else if (IsEqualIID(riid
, &IID_IAMFilterMiscFlags
))
422 *ppv
= &This
->IAMFilterMiscFlags_iface
;
426 IUnknown_AddRef((IUnknown
*)(*ppv
));
430 if (!IsEqualIID(riid
, &IID_IPin
) && !IsEqualIID(riid
, &IID_IMediaSeeking
) &&
431 !IsEqualIID(riid
, &IID_IVideoWindow
) && !IsEqualIID(riid
, &IID_IBasicAudio
))
432 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
434 return E_NOINTERFACE
;
437 static ULONG WINAPI
AsyncReader_Release(IBaseFilter
* iface
)
439 AsyncReader
*This
= impl_from_IBaseFilter(iface
);
440 ULONG refCount
= BaseFilterImpl_Release(iface
);
442 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
446 if (This
->pOutputPin
)
449 if(SUCCEEDED(IPin_ConnectedTo(This
->pOutputPin
, &pConnectedTo
)))
451 IPin_Disconnect(pConnectedTo
);
452 IPin_Release(pConnectedTo
);
454 IPin_Disconnect(This
->pOutputPin
);
455 IPin_Release(This
->pOutputPin
);
457 CoTaskMemFree(This
->pszFileName
);
459 FreeMediaType(This
->pmt
);
467 /** IMediaFilter methods **/
469 static HRESULT WINAPI
AsyncReader_Stop(IBaseFilter
* iface
)
471 AsyncReader
*This
= impl_from_IBaseFilter(iface
);
475 This
->filter
.state
= State_Stopped
;
480 static HRESULT WINAPI
AsyncReader_Pause(IBaseFilter
* iface
)
482 AsyncReader
*This
= impl_from_IBaseFilter(iface
);
486 This
->filter
.state
= State_Paused
;
491 static HRESULT WINAPI
AsyncReader_Run(IBaseFilter
* iface
, REFERENCE_TIME tStart
)
493 AsyncReader
*This
= impl_from_IBaseFilter(iface
);
495 TRACE("(%x%08x)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
);
497 This
->filter
.state
= State_Running
;
502 /** IBaseFilter methods **/
504 static HRESULT WINAPI
AsyncReader_FindPin(IBaseFilter
* iface
, LPCWSTR Id
, IPin
**ppPin
)
506 AsyncReader
*This
= impl_from_IBaseFilter(iface
);
507 TRACE("(%s, %p)\n", debugstr_w(Id
), ppPin
);
512 if (strcmpW(Id
, wszOutputPinName
))
515 return VFW_E_NOT_FOUND
;
518 *ppPin
= This
->pOutputPin
;
523 static const IBaseFilterVtbl AsyncReader_Vtbl
=
525 AsyncReader_QueryInterface
,
526 BaseFilterImpl_AddRef
,
528 BaseFilterImpl_GetClassID
,
532 BaseFilterImpl_GetState
,
533 BaseFilterImpl_SetSyncSource
,
534 BaseFilterImpl_GetSyncSource
,
535 BaseFilterImpl_EnumPins
,
537 BaseFilterImpl_QueryFilterInfo
,
538 BaseFilterImpl_JoinFilterGraph
,
539 BaseFilterImpl_QueryVendorInfo
542 static HRESULT WINAPI
FileSource_QueryInterface(IFileSourceFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
544 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
546 return IBaseFilter_QueryInterface(&This
->filter
.IBaseFilter_iface
, riid
, ppv
);
549 static ULONG WINAPI
FileSource_AddRef(IFileSourceFilter
* iface
)
551 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
553 return IBaseFilter_AddRef(&This
->filter
.IBaseFilter_iface
);
556 static ULONG WINAPI
FileSource_Release(IFileSourceFilter
* iface
)
558 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
560 return IBaseFilter_Release(&This
->filter
.IBaseFilter_iface
);
563 static HRESULT WINAPI
FileSource_Load(IFileSourceFilter
* iface
, LPCOLESTR pszFileName
, const AM_MEDIA_TYPE
* pmt
)
567 IAsyncReader
* pReader
= NULL
;
568 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
570 TRACE("(%s, %p)\n", debugstr_w(pszFileName
), pmt
);
573 /* FIXME: check the sharing values that native uses */
574 hFile
= CreateFileW(pszFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, NULL
);
576 if (hFile
== INVALID_HANDLE_VALUE
)
578 return HRESULT_FROM_WIN32(GetLastError());
582 hr
= FileAsyncReader_Construct(hFile
, &This
->filter
.IBaseFilter_iface
, &This
->filter
.csFilter
, &This
->pOutputPin
);
583 BaseFilterImpl_IncrementPinVersion(&This
->filter
);
586 hr
= IPin_QueryInterface(This
->pOutputPin
, &IID_IAsyncReader
, (LPVOID
*)&pReader
);
588 /* store file name & media type */
591 CoTaskMemFree(This
->pszFileName
);
593 FreeMediaType(This
->pmt
);
595 This
->pszFileName
= CoTaskMemAlloc((strlenW(pszFileName
) + 1) * sizeof(WCHAR
));
596 strcpyW(This
->pszFileName
, pszFileName
);
598 This
->pmt
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
));
601 This
->pmt
->bFixedSizeSamples
= TRUE
;
602 This
->pmt
->bTemporalCompression
= FALSE
;
603 This
->pmt
->cbFormat
= 0;
604 This
->pmt
->pbFormat
= NULL
;
605 This
->pmt
->pUnk
= NULL
;
606 This
->pmt
->lSampleSize
= 0;
607 This
->pmt
->formattype
= FORMAT_None
;
608 hr
= GetClassMediaFile(pReader
, pszFileName
, &This
->pmt
->majortype
, &This
->pmt
->subtype
);
611 CoTaskMemFree(This
->pmt
);
616 CopyMediaType(This
->pmt
, pmt
);
620 IAsyncReader_Release(pReader
);
624 if (This
->pOutputPin
)
626 IPin_Release(This
->pOutputPin
);
627 This
->pOutputPin
= NULL
;
630 CoTaskMemFree(This
->pszFileName
);
632 FreeMediaType(This
->pmt
);
633 This
->pszFileName
= NULL
;
639 /* FIXME: check return codes */
643 static HRESULT WINAPI
FileSource_GetCurFile(IFileSourceFilter
* iface
, LPOLESTR
* ppszFileName
, AM_MEDIA_TYPE
* pmt
)
645 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
647 TRACE("(%p, %p)\n", ppszFileName
, pmt
);
652 /* copy file name & media type if available, otherwise clear the outputs */
653 if (This
->pszFileName
)
655 *ppszFileName
= CoTaskMemAlloc((strlenW(This
->pszFileName
) + 1) * sizeof(WCHAR
));
656 strcpyW(*ppszFileName
, This
->pszFileName
);
659 *ppszFileName
= NULL
;
664 CopyMediaType(pmt
, This
->pmt
);
666 ZeroMemory(pmt
, sizeof(*pmt
));
672 static const IFileSourceFilterVtbl FileSource_Vtbl
=
674 FileSource_QueryInterface
,
678 FileSource_GetCurFile
682 /* the dwUserData passed back to user */
683 typedef struct DATAREQUEST
685 IMediaSample
* pSample
; /* sample passed to us by user */
686 DWORD_PTR dwUserData
; /* user data passed to us */
687 OVERLAPPED ovl
; /* our overlapped structure */
690 typedef struct FileAsyncReader
693 IAsyncReader IAsyncReader_iface
;
695 ALLOCATOR_PROPERTIES allocProps
;
698 /* Why would you need more? Every sample has its own handle */
702 CRITICAL_SECTION csList
; /* critical section to prevent concurrency issues */
703 DATAREQUEST
*sample_list
;
705 /* Have a handle for every sample, and then one more as flushing handle */
709 static inline FileAsyncReader
*impl_from_IPin(IPin
*iface
)
711 return CONTAINING_RECORD(iface
, FileAsyncReader
, pin
.pin
.IPin_iface
);
714 static inline FileAsyncReader
*impl_from_BasePin(BasePin
*iface
)
716 return CONTAINING_RECORD(iface
, FileAsyncReader
, pin
.pin
);
719 static inline FileAsyncReader
*impl_from_BaseOutputPin(BaseOutputPin
*iface
)
721 return CONTAINING_RECORD(iface
, FileAsyncReader
, pin
);
724 static inline BaseOutputPin
*impl_BaseOututPin_from_BasePin(BasePin
*iface
)
726 return CONTAINING_RECORD(iface
, BaseOutputPin
, pin
);
729 static inline FileAsyncReader
*impl_from_IAsyncReader(IAsyncReader
*iface
)
731 return CONTAINING_RECORD(iface
, FileAsyncReader
, IAsyncReader_iface
);
734 static HRESULT WINAPI
FileAsyncReaderPin_QueryAccept(IPin
*iface
, const AM_MEDIA_TYPE
*pmt
)
736 FileAsyncReader
*This
= impl_from_IPin(iface
);
737 AM_MEDIA_TYPE
*pmt_filter
= impl_from_IBaseFilter(This
->pin
.pin
.pinInfo
.pFilter
)->pmt
;
739 FIXME("(%p, %p)\n", iface
, pmt
);
741 if (IsEqualGUID(&pmt
->majortype
, &pmt_filter
->majortype
) &&
742 IsEqualGUID(&pmt
->subtype
, &pmt_filter
->subtype
) &&
743 IsEqualGUID(&pmt
->formattype
, &FORMAT_None
))
749 static HRESULT WINAPI
FileAsyncReaderPin_GetMediaType(BasePin
*iface
, int iPosition
, AM_MEDIA_TYPE
*pmt
)
751 FileAsyncReader
*This
= impl_from_BasePin(iface
);
755 return VFW_S_NO_MORE_ITEMS
;
756 CopyMediaType(pmt
, impl_from_IBaseFilter(This
->pin
.pin
.pinInfo
.pFilter
)->pmt
);
760 /* overridden pin functions */
762 static HRESULT WINAPI
FileAsyncReaderPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
764 FileAsyncReader
*This
= impl_from_IPin(iface
);
765 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
769 if (IsEqualIID(riid
, &IID_IUnknown
))
771 else if (IsEqualIID(riid
, &IID_IPin
))
773 else if (IsEqualIID(riid
, &IID_IAsyncReader
))
774 *ppv
= &This
->IAsyncReader_iface
;
778 IUnknown_AddRef((IUnknown
*)(*ppv
));
782 if (!IsEqualIID(riid
, &IID_IPin
) && !IsEqualIID(riid
, &IID_IMediaSeeking
))
783 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
785 return E_NOINTERFACE
;
788 static ULONG WINAPI
FileAsyncReaderPin_Release(IPin
* iface
)
790 FileAsyncReader
*This
= impl_from_IPin(iface
);
791 ULONG refCount
= InterlockedDecrement(&This
->pin
.pin
.refCount
);
794 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
798 CoTaskMemFree(This
->sample_list
);
799 if (This
->handle_list
)
801 for (x
= 0; x
<= This
->samples
; ++x
)
802 CloseHandle(This
->handle_list
[x
]);
803 CoTaskMemFree(This
->handle_list
);
805 CloseHandle(This
->hFile
);
806 This
->csList
.DebugInfo
->Spare
[0] = 0;
807 DeleteCriticalSection(&This
->csList
);
814 static const IPinVtbl FileAsyncReaderPin_Vtbl
=
816 FileAsyncReaderPin_QueryInterface
,
818 FileAsyncReaderPin_Release
,
819 BaseOutputPinImpl_Connect
,
820 BaseOutputPinImpl_ReceiveConnection
,
821 BasePinImpl_Disconnect
,
822 BasePinImpl_ConnectedTo
,
823 BasePinImpl_ConnectionMediaType
,
824 BasePinImpl_QueryPinInfo
,
825 BasePinImpl_QueryDirection
,
827 FileAsyncReaderPin_QueryAccept
,
828 BasePinImpl_EnumMediaTypes
,
829 BasePinImpl_QueryInternalConnections
,
830 BaseOutputPinImpl_EndOfStream
,
831 BaseOutputPinImpl_BeginFlush
,
832 BaseOutputPinImpl_EndFlush
,
833 BasePinImpl_NewSegment
836 /* Function called as a helper to IPin_Connect */
837 /* specific AM_MEDIA_TYPE - it cannot be NULL */
838 /* this differs from standard OutputPin_AttemptConnection only in that it
839 * doesn't need the IMemInputPin interface on the receiving pin */
840 static HRESULT WINAPI
FileAsyncReaderPin_AttemptConnection(BasePin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
842 BaseOutputPin
*This
= impl_BaseOututPin_from_BasePin(iface
);
845 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
846 dump_AM_MEDIA_TYPE(pmt
);
848 /* FIXME: call queryacceptproc */
850 This
->pin
.pConnectedTo
= pReceivePin
;
851 IPin_AddRef(pReceivePin
);
852 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
854 hr
= IPin_ReceiveConnection(pReceivePin
, &iface
->IPin_iface
, pmt
);
858 IPin_Release(This
->pin
.pConnectedTo
);
859 This
->pin
.pConnectedTo
= NULL
;
860 FreeMediaType(&This
->pin
.mtCurrent
);
863 TRACE(" -- %x\n", hr
);
867 static HRESULT WINAPI
FileAsyncReaderPin_DecideBufferSize(BaseOutputPin
*iface
, IMemAllocator
*pAlloc
, ALLOCATOR_PROPERTIES
*ppropInputRequest
)
869 FileAsyncReader
*This
= impl_from_BaseOutputPin(iface
);
870 ALLOCATOR_PROPERTIES actual
;
872 if (ppropInputRequest
->cbAlign
&& ppropInputRequest
->cbAlign
!= This
->allocProps
.cbAlign
)
873 FIXME("Requested Buffer cbAlign mismatch %i,%i\n",This
->allocProps
.cbAlign
, ppropInputRequest
->cbAlign
);
874 if (ppropInputRequest
->cbPrefix
)
875 FIXME("Requested Buffer cbPrefix mismatch %i,%i\n",This
->allocProps
.cbPrefix
, ppropInputRequest
->cbPrefix
);
876 if (ppropInputRequest
->cbBuffer
)
877 FIXME("Requested Buffer cbBuffer mismatch %i,%i\n",This
->allocProps
.cbBuffer
, ppropInputRequest
->cbBuffer
);
878 if (ppropInputRequest
->cBuffers
)
879 FIXME("Requested Buffer cBuffers mismatch %i,%i\n",This
->allocProps
.cBuffers
, ppropInputRequest
->cBuffers
);
881 return IMemAllocator_SetProperties(pAlloc
, &This
->allocProps
, &actual
);
884 static const BasePinFuncTable output_BaseFuncTable
= {
886 FileAsyncReaderPin_AttemptConnection
,
887 BasePinImpl_GetMediaTypeVersion
,
888 FileAsyncReaderPin_GetMediaType
891 static const BaseOutputPinFuncTable output_BaseOutputFuncTable
= {
892 FileAsyncReaderPin_DecideBufferSize
,
893 BaseOutputPinImpl_DecideAllocator
,
894 BaseOutputPinImpl_BreakConnect
897 static HRESULT
FileAsyncReader_Construct(HANDLE hFile
, IBaseFilter
* pBaseFilter
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
903 piOutput
.dir
= PINDIR_OUTPUT
;
904 piOutput
.pFilter
= pBaseFilter
;
905 strcpyW(piOutput
.achName
, wszOutputPinName
);
906 hr
= BaseOutputPin_Construct(&FileAsyncReaderPin_Vtbl
, sizeof(FileAsyncReader
), &piOutput
, &output_BaseFuncTable
, &output_BaseOutputFuncTable
, pCritSec
, ppPin
);
910 FileAsyncReader
*pPinImpl
= (FileAsyncReader
*)*ppPin
;
911 pPinImpl
->IAsyncReader_iface
.lpVtbl
= &FileAsyncReader_Vtbl
;
912 pPinImpl
->hFile
= hFile
;
913 pPinImpl
->bFlushing
= FALSE
;
914 pPinImpl
->sample_list
= NULL
;
915 pPinImpl
->handle_list
= NULL
;
916 pPinImpl
->queued_number
= 0;
917 InitializeCriticalSection(&pPinImpl
->csList
);
918 pPinImpl
->csList
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FileAsyncReader.csList");
925 static HRESULT WINAPI
FileAsyncReader_QueryInterface(IAsyncReader
* iface
, REFIID riid
, LPVOID
* ppv
)
927 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
929 return IPin_QueryInterface(&This
->pin
.pin
.IPin_iface
, riid
, ppv
);
932 static ULONG WINAPI
FileAsyncReader_AddRef(IAsyncReader
* iface
)
934 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
936 return IPin_AddRef(&This
->pin
.pin
.IPin_iface
);
939 static ULONG WINAPI
FileAsyncReader_Release(IAsyncReader
* iface
)
941 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
943 return IPin_Release(&This
->pin
.pin
.IPin_iface
);
946 #define DEF_ALIGNMENT 1
948 static HRESULT WINAPI
FileAsyncReader_RequestAllocator(IAsyncReader
* iface
, IMemAllocator
* pPreferred
, ALLOCATOR_PROPERTIES
* pProps
, IMemAllocator
** ppActual
)
950 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
954 TRACE("(%p, %p, %p)\n", pPreferred
, pProps
, ppActual
);
956 if (!pProps
->cbAlign
|| (pProps
->cbAlign
% DEF_ALIGNMENT
) != 0)
957 pProps
->cbAlign
= DEF_ALIGNMENT
;
961 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, pProps
);
962 /* FIXME: check we are still aligned */
965 IMemAllocator_AddRef(pPreferred
);
966 *ppActual
= pPreferred
;
967 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr
);
974 hr
= CoCreateInstance(&CLSID_MemoryAllocator
, NULL
, CLSCTX_INPROC
, &IID_IMemAllocator
, (LPVOID
*)&pPreferred
);
978 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, pProps
);
979 /* FIXME: check we are still aligned */
982 *ppActual
= pPreferred
;
983 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr
);
990 CoTaskMemFree(This
->sample_list
);
991 if (This
->handle_list
)
994 for (x
= 0; x
<= This
->samples
; ++x
)
995 CloseHandle(This
->handle_list
[x
]);
996 CoTaskMemFree(This
->handle_list
);
999 This
->samples
= pProps
->cBuffers
;
1000 This
->oldest_sample
= 0;
1001 TRACE("Samples: %u\n", This
->samples
);
1002 This
->sample_list
= CoTaskMemAlloc(sizeof(This
->sample_list
[0]) * pProps
->cBuffers
);
1003 This
->handle_list
= CoTaskMemAlloc(sizeof(HANDLE
) * pProps
->cBuffers
* 2);
1005 if (This
->sample_list
&& This
->handle_list
)
1008 ZeroMemory(This
->sample_list
, sizeof(This
->sample_list
[0]) * pProps
->cBuffers
);
1009 for (x
= 0; x
< This
->samples
; ++x
)
1011 This
->sample_list
[x
].ovl
.hEvent
= This
->handle_list
[x
] = CreateEventW(NULL
, 0, 0, NULL
);
1012 if (x
+ 1 < This
->samples
)
1013 This
->handle_list
[This
->samples
+ 1 + x
] = This
->handle_list
[x
];
1015 This
->handle_list
[This
->samples
] = CreateEventW(NULL
, 1, 0, NULL
);
1016 This
->allocProps
= *pProps
;
1021 CoTaskMemFree(This
->sample_list
);
1022 CoTaskMemFree(This
->handle_list
);
1024 This
->sample_list
= NULL
;
1025 This
->handle_list
= NULL
;
1033 IMemAllocator_Release(pPreferred
);
1036 TRACE("-- %x\n", hr
);
1040 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
1041 * however, this would be quite complicated to do and may be a bit error prone */
1042 static HRESULT WINAPI
FileAsyncReader_Request(IAsyncReader
* iface
, IMediaSample
* pSample
, DWORD_PTR dwUser
)
1045 REFERENCE_TIME Start
;
1046 REFERENCE_TIME Stop
;
1047 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1048 LPBYTE pBuffer
= NULL
;
1050 TRACE("(%p, %lx)\n", pSample
, dwUser
);
1055 /* get start and stop positions in bytes */
1057 hr
= IMediaSample_GetTime(pSample
, &Start
, &Stop
);
1060 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
1062 EnterCriticalSection(&This
->csList
);
1063 if (This
->bFlushing
)
1065 LeaveCriticalSection(&This
->csList
);
1066 return VFW_E_WRONG_STATE
;
1071 DWORD dwLength
= (DWORD
) BYTES_FROM_MEDIATIME(Stop
- Start
);
1072 DATAREQUEST
*pDataRq
;
1075 /* Try to insert above the waiting sample if possible */
1076 for (x
= This
->oldest_sample
; x
< This
->samples
; ++x
)
1078 if (!This
->sample_list
[x
].pSample
)
1082 if (x
>= This
->samples
)
1083 for (x
= 0; x
< This
->oldest_sample
; ++x
)
1085 if (!This
->sample_list
[x
].pSample
)
1089 /* There must be a sample we have found */
1090 assert(x
< This
->samples
);
1091 ++This
->queued_number
;
1093 pDataRq
= This
->sample_list
+ x
;
1095 pDataRq
->ovl
.u
.s
.Offset
= (DWORD
) BYTES_FROM_MEDIATIME(Start
);
1096 pDataRq
->ovl
.u
.s
.OffsetHigh
= (DWORD
)(BYTES_FROM_MEDIATIME(Start
) >> (sizeof(DWORD
) * 8));
1097 pDataRq
->dwUserData
= dwUser
;
1099 /* we violate traditional COM rules here by maintaining
1100 * a reference to the sample, but not calling AddRef, but
1101 * that's what MSDN says to do */
1102 pDataRq
->pSample
= pSample
;
1104 /* this is definitely not how it is implemented on Win9x
1105 * as they do not support async reads on files, but it is
1106 * sooo much easier to use this than messing around with threads!
1108 if (!ReadFile(This
->hFile
, pBuffer
, dwLength
, NULL
, &pDataRq
->ovl
))
1109 hr
= HRESULT_FROM_WIN32(GetLastError());
1111 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1112 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1116 LeaveCriticalSection(&This
->csList
);
1118 TRACE("-- %x\n", hr
);
1122 static HRESULT WINAPI
FileAsyncReader_WaitForNext(IAsyncReader
* iface
, DWORD dwTimeout
, IMediaSample
** ppSample
, DWORD_PTR
* pdwUser
)
1125 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1128 TRACE("(%u, %p, %p)\n", dwTimeout
, ppSample
, pdwUser
);
1133 EnterCriticalSection(&This
->csList
);
1134 if (!This
->bFlushing
)
1136 LONG oldest
= This
->oldest_sample
;
1138 if (!This
->queued_number
)
1140 /* It could be that nothing is queued right now, but that can be fixed */
1141 WARN("Called without samples in queue and not flushing!!\n");
1143 LeaveCriticalSection(&This
->csList
);
1145 /* wait for an object to read, or time out */
1146 buffer
= WaitForMultipleObjectsEx(This
->samples
+1, This
->handle_list
+ oldest
, FALSE
, dwTimeout
, TRUE
);
1148 EnterCriticalSection(&This
->csList
);
1149 if (buffer
<= This
->samples
)
1151 /* Re-scale the buffer back to normal */
1154 /* Uh oh, we overshot the flusher handle, renormalize it back to 0..Samples-1 */
1155 if (buffer
> This
->samples
)
1156 buffer
-= This
->samples
+ 1;
1157 assert(buffer
<= This
->samples
);
1160 if (buffer
>= This
->samples
)
1162 if (buffer
!= This
->samples
)
1164 FIXME("Returned: %u (%08x)\n", buffer
, GetLastError());
1168 hr
= VFW_E_WRONG_STATE
;
1172 --This
->queued_number
;
1175 if (This
->bFlushing
&& buffer
== ~0)
1177 for (buffer
= 0; buffer
< This
->samples
; ++buffer
)
1179 if (This
->sample_list
[buffer
].pSample
)
1181 ResetEvent(This
->handle_list
[buffer
]);
1185 if (buffer
== This
->samples
)
1187 assert(!This
->queued_number
);
1192 --This
->queued_number
;
1199 REFERENCE_TIME rtStart
, rtStop
;
1200 REFERENCE_TIME rtSampleStart
, rtSampleStop
;
1201 DATAREQUEST
*pDataRq
= This
->sample_list
+ buffer
;
1204 /* get any errors */
1205 if (!This
->bFlushing
&& !GetOverlappedResult(This
->hFile
, &pDataRq
->ovl
, &dwBytes
, FALSE
))
1206 hr
= HRESULT_FROM_WIN32(GetLastError());
1208 /* Return the sample no matter what so it can be destroyed */
1209 *ppSample
= pDataRq
->pSample
;
1210 *pdwUser
= pDataRq
->dwUserData
;
1212 if (This
->bFlushing
)
1213 hr
= VFW_E_WRONG_STATE
;
1218 /* Set the time on the sample */
1219 IMediaSample_SetActualDataLength(pDataRq
->pSample
, dwBytes
);
1221 rtStart
= (DWORD64
)pDataRq
->ovl
.u
.s
.Offset
+ ((DWORD64
)pDataRq
->ovl
.u
.s
.OffsetHigh
<< 32);
1222 rtStart
= MEDIATIME_FROM_BYTES(rtStart
);
1223 rtStop
= rtStart
+ MEDIATIME_FROM_BYTES(dwBytes
);
1225 IMediaSample_GetTime(pDataRq
->pSample
, &rtSampleStart
, &rtSampleStop
);
1226 assert(rtStart
== rtSampleStart
);
1227 assert(rtStop
<= rtSampleStop
);
1229 IMediaSample_SetTime(pDataRq
->pSample
, &rtStart
, &rtStop
);
1230 assert(rtStart
== rtSampleStart
);
1232 assert(rtStop
== rtSampleStop
);
1234 assert(rtStop
== rtStart
);
1236 This
->sample_list
[buffer
].pSample
= NULL
;
1237 assert(This
->oldest_sample
< This
->samples
);
1239 if (buffer
== This
->oldest_sample
)
1242 for (x
= This
->oldest_sample
+ 1; x
< This
->samples
; ++x
)
1243 if (This
->sample_list
[x
].pSample
)
1245 if (x
>= This
->samples
)
1246 for (x
= 0; x
< This
->oldest_sample
; ++x
)
1247 if (This
->sample_list
[x
].pSample
)
1249 if (This
->oldest_sample
== x
)
1250 /* No samples found, reset to 0 */
1252 This
->oldest_sample
= x
;
1255 LeaveCriticalSection(&This
->csList
);
1257 TRACE("-- %x\n", hr
);
1261 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
);
1263 static HRESULT WINAPI
FileAsyncReader_SyncReadAligned(IAsyncReader
* iface
, IMediaSample
* pSample
)
1266 REFERENCE_TIME tStart
;
1267 REFERENCE_TIME tStop
;
1270 TRACE("(%p)\n", pSample
);
1272 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
1275 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
1278 hr
= FileAsyncReader_SyncRead(iface
,
1279 BYTES_FROM_MEDIATIME(tStart
),
1280 (LONG
) BYTES_FROM_MEDIATIME(tStop
- tStart
),
1283 TRACE("-- %x\n", hr
);
1287 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
)
1291 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1293 TRACE("(%x%08x, %d, %p)\n", (ULONG
)(llPosition
>> 32), (ULONG
)llPosition
, lLength
, pBuffer
);
1295 ZeroMemory(&ovl
, sizeof(ovl
));
1297 ovl
.hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
1298 /* NOTE: llPosition is the actual byte position to start reading from */
1299 ovl
.u
.s
.Offset
= (DWORD
) llPosition
;
1300 ovl
.u
.s
.OffsetHigh
= (DWORD
) (llPosition
>> (sizeof(DWORD
) * 8));
1302 if (!ReadFile(This
->hFile
, pBuffer
, lLength
, NULL
, &ovl
))
1303 hr
= HRESULT_FROM_WIN32(GetLastError());
1305 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1312 if (!GetOverlappedResult(This
->hFile
, &ovl
, &dwBytesRead
, TRUE
))
1313 hr
= HRESULT_FROM_WIN32(GetLastError());
1316 CloseHandle(ovl
.hEvent
);
1318 TRACE("-- %x\n", hr
);
1322 static HRESULT WINAPI
FileAsyncReader_Length(IAsyncReader
* iface
, LONGLONG
* pTotal
, LONGLONG
* pAvailable
)
1326 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1328 TRACE("(%p, %p)\n", pTotal
, pAvailable
);
1330 if (((dwSizeLow
= GetFileSize(This
->hFile
, &dwSizeHigh
)) == -1) &&
1331 (GetLastError() != NO_ERROR
))
1332 return HRESULT_FROM_WIN32(GetLastError());
1334 *pTotal
= (LONGLONG
)dwSizeLow
| (LONGLONG
)dwSizeHigh
<< (sizeof(DWORD
) * 8);
1336 *pAvailable
= *pTotal
;
1341 static HRESULT WINAPI
FileAsyncReader_BeginFlush(IAsyncReader
* iface
)
1343 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1347 EnterCriticalSection(&This
->csList
);
1348 This
->bFlushing
= TRUE
;
1349 CancelIo(This
->hFile
);
1350 SetEvent(This
->handle_list
[This
->samples
]);
1351 LeaveCriticalSection(&This
->csList
);
1356 static HRESULT WINAPI
FileAsyncReader_EndFlush(IAsyncReader
* iface
)
1358 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1363 EnterCriticalSection(&This
->csList
);
1364 ResetEvent(This
->handle_list
[This
->samples
]);
1365 This
->bFlushing
= FALSE
;
1366 for (x
= 0; x
< This
->samples
; ++x
)
1367 assert(!This
->sample_list
[x
].pSample
);
1369 LeaveCriticalSection(&This
->csList
);
1374 static const IAsyncReaderVtbl FileAsyncReader_Vtbl
=
1376 FileAsyncReader_QueryInterface
,
1377 FileAsyncReader_AddRef
,
1378 FileAsyncReader_Release
,
1379 FileAsyncReader_RequestAllocator
,
1380 FileAsyncReader_Request
,
1381 FileAsyncReader_WaitForNext
,
1382 FileAsyncReader_SyncReadAligned
,
1383 FileAsyncReader_SyncRead
,
1384 FileAsyncReader_Length
,
1385 FileAsyncReader_BeginFlush
,
1386 FileAsyncReader_EndFlush
,
1390 static HRESULT WINAPI
AMFilterMiscFlags_QueryInterface(IAMFilterMiscFlags
*iface
, REFIID riid
, void **ppv
) {
1391 AsyncReader
*This
= impl_from_IAMFilterMiscFlags(iface
);
1392 return IUnknown_QueryInterface((IUnknown
*)This
, riid
, ppv
);
1395 static ULONG WINAPI
AMFilterMiscFlags_AddRef(IAMFilterMiscFlags
*iface
) {
1396 AsyncReader
*This
= impl_from_IAMFilterMiscFlags(iface
);
1397 return IUnknown_AddRef((IUnknown
*)This
);
1400 static ULONG WINAPI
AMFilterMiscFlags_Release(IAMFilterMiscFlags
*iface
) {
1401 AsyncReader
*This
= impl_from_IAMFilterMiscFlags(iface
);
1402 return IUnknown_Release((IUnknown
*)This
);
1405 static ULONG WINAPI
AMFilterMiscFlags_GetMiscFlags(IAMFilterMiscFlags
*iface
) {
1406 return AM_FILTER_MISC_FLAGS_IS_SOURCE
;
1409 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl
= {
1410 AMFilterMiscFlags_QueryInterface
,
1411 AMFilterMiscFlags_AddRef
,
1412 AMFilterMiscFlags_Release
,
1413 AMFilterMiscFlags_GetMiscFlags