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};
311 if ((temp
= RegEnumValueW(hkeyMinor
, indexValue
, wszValueName
, &dwValueNameLen
, NULL
, &dwType
, (LPBYTE
)wszPatternString
, &dwDataLen
)) != ERROR_SUCCESS
)
313 HeapFree(GetProcessHeap(), 0, wszPatternString
);
317 /* if it is not the source filter value */
318 if (strcmpW(wszValueName
, wszSourceFilter
))
320 if (process_pattern_string(wszPatternString
, pReader
) == S_OK
)
322 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName
, majorType
)) &&
323 SUCCEEDED(CLSIDFromString(wszMinorKeyName
, minorType
)))
327 HeapFree(GetProcessHeap(), 0, wszPatternString
);
329 CloseHandle(hkeyMinor
);
332 CloseHandle(hkeyMajor
);
335 CloseHandle(hkeyMediaType
);
337 if (SUCCEEDED(hr
) && !bFound
)
339 ERR("Media class not found\n");
343 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType
), qzdebugstr_guid(minorType
));
348 static IPin
* WINAPI
AsyncReader_GetPin(BaseFilter
*iface
, int pos
)
350 AsyncReader
*This
= impl_from_BaseFilter(iface
);
352 if (pos
>= 1 || !This
->pOutputPin
)
355 IPin_AddRef(This
->pOutputPin
);
356 return This
->pOutputPin
;
359 static LONG WINAPI
AsyncReader_GetPinCount(BaseFilter
*iface
)
361 AsyncReader
*This
= impl_from_BaseFilter(iface
);
363 if (!This
->pOutputPin
)
369 static const BaseFilterFuncTable BaseFuncTable
= {
371 AsyncReader_GetPinCount
374 HRESULT
AsyncReader_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
376 AsyncReader
*pAsyncRead
;
379 return CLASS_E_NOAGGREGATION
;
381 pAsyncRead
= CoTaskMemAlloc(sizeof(AsyncReader
));
384 return E_OUTOFMEMORY
;
386 BaseFilter_Init(&pAsyncRead
->filter
, &AsyncReader_Vtbl
, &CLSID_AsyncReader
, (DWORD_PTR
)(__FILE__
": AsyncReader.csFilter"), &BaseFuncTable
);
388 pAsyncRead
->IFileSourceFilter_iface
.lpVtbl
= &FileSource_Vtbl
;
389 pAsyncRead
->IAMFilterMiscFlags_iface
.lpVtbl
= &IAMFilterMiscFlags_Vtbl
;
390 pAsyncRead
->pOutputPin
= NULL
;
392 pAsyncRead
->pszFileName
= NULL
;
393 pAsyncRead
->pmt
= NULL
;
397 TRACE("-- created at %p\n", pAsyncRead
);
402 /** IUnknown methods **/
404 static HRESULT WINAPI
AsyncReader_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
406 AsyncReader
*This
= impl_from_IBaseFilter(iface
);
408 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
412 if (IsEqualIID(riid
, &IID_IUnknown
))
414 else if (IsEqualIID(riid
, &IID_IPersist
))
416 else if (IsEqualIID(riid
, &IID_IMediaFilter
))
418 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
420 else if (IsEqualIID(riid
, &IID_IFileSourceFilter
))
421 *ppv
= &This
->IFileSourceFilter_iface
;
422 else if (IsEqualIID(riid
, &IID_IAMFilterMiscFlags
))
423 *ppv
= &This
->IAMFilterMiscFlags_iface
;
427 IUnknown_AddRef((IUnknown
*)(*ppv
));
431 if (!IsEqualIID(riid
, &IID_IPin
) && !IsEqualIID(riid
, &IID_IMediaSeeking
) &&
432 !IsEqualIID(riid
, &IID_IVideoWindow
) && !IsEqualIID(riid
, &IID_IBasicAudio
))
433 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
435 return E_NOINTERFACE
;
438 static ULONG WINAPI
AsyncReader_Release(IBaseFilter
* iface
)
440 AsyncReader
*This
= impl_from_IBaseFilter(iface
);
441 ULONG refCount
= BaseFilterImpl_Release(iface
);
443 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
447 if (This
->pOutputPin
)
450 if(SUCCEEDED(IPin_ConnectedTo(This
->pOutputPin
, &pConnectedTo
)))
452 IPin_Disconnect(pConnectedTo
);
453 IPin_Release(pConnectedTo
);
455 IPin_Disconnect(This
->pOutputPin
);
456 IPin_Release(This
->pOutputPin
);
458 CoTaskMemFree(This
->pszFileName
);
460 FreeMediaType(This
->pmt
);
468 /** IMediaFilter methods **/
470 static HRESULT WINAPI
AsyncReader_Stop(IBaseFilter
* iface
)
472 AsyncReader
*This
= impl_from_IBaseFilter(iface
);
476 This
->filter
.state
= State_Stopped
;
481 static HRESULT WINAPI
AsyncReader_Pause(IBaseFilter
* iface
)
483 AsyncReader
*This
= impl_from_IBaseFilter(iface
);
487 This
->filter
.state
= State_Paused
;
492 static HRESULT WINAPI
AsyncReader_Run(IBaseFilter
* iface
, REFERENCE_TIME tStart
)
494 AsyncReader
*This
= impl_from_IBaseFilter(iface
);
496 TRACE("(%x%08x)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
);
498 This
->filter
.state
= State_Running
;
503 /** IBaseFilter methods **/
505 static HRESULT WINAPI
AsyncReader_FindPin(IBaseFilter
* iface
, LPCWSTR Id
, IPin
**ppPin
)
507 AsyncReader
*This
= impl_from_IBaseFilter(iface
);
508 TRACE("(%s, %p)\n", debugstr_w(Id
), ppPin
);
513 if (strcmpW(Id
, wszOutputPinName
))
516 return VFW_E_NOT_FOUND
;
519 *ppPin
= This
->pOutputPin
;
520 IUnknown_AddRef(*ppPin
);
524 static const IBaseFilterVtbl AsyncReader_Vtbl
=
526 AsyncReader_QueryInterface
,
527 BaseFilterImpl_AddRef
,
529 BaseFilterImpl_GetClassID
,
533 BaseFilterImpl_GetState
,
534 BaseFilterImpl_SetSyncSource
,
535 BaseFilterImpl_GetSyncSource
,
536 BaseFilterImpl_EnumPins
,
538 BaseFilterImpl_QueryFilterInfo
,
539 BaseFilterImpl_JoinFilterGraph
,
540 BaseFilterImpl_QueryVendorInfo
543 static HRESULT WINAPI
FileSource_QueryInterface(IFileSourceFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
545 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
547 return IBaseFilter_QueryInterface(&This
->filter
.IBaseFilter_iface
, riid
, ppv
);
550 static ULONG WINAPI
FileSource_AddRef(IFileSourceFilter
* iface
)
552 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
554 return IBaseFilter_AddRef(&This
->filter
.IBaseFilter_iface
);
557 static ULONG WINAPI
FileSource_Release(IFileSourceFilter
* iface
)
559 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
561 return IBaseFilter_Release(&This
->filter
.IBaseFilter_iface
);
564 static HRESULT WINAPI
FileSource_Load(IFileSourceFilter
* iface
, LPCOLESTR pszFileName
, const AM_MEDIA_TYPE
* pmt
)
568 IAsyncReader
* pReader
= NULL
;
569 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
571 TRACE("(%s, %p)\n", debugstr_w(pszFileName
), pmt
);
574 /* FIXME: check the sharing values that native uses */
575 hFile
= CreateFileW(pszFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, NULL
);
577 if (hFile
== INVALID_HANDLE_VALUE
)
579 return HRESULT_FROM_WIN32(GetLastError());
583 hr
= FileAsyncReader_Construct(hFile
, &This
->filter
.IBaseFilter_iface
, &This
->filter
.csFilter
, &This
->pOutputPin
);
584 BaseFilterImpl_IncrementPinVersion(&This
->filter
);
587 hr
= IPin_QueryInterface(This
->pOutputPin
, &IID_IAsyncReader
, (LPVOID
*)&pReader
);
589 /* store file name & media type */
592 CoTaskMemFree(This
->pszFileName
);
594 FreeMediaType(This
->pmt
);
596 This
->pszFileName
= CoTaskMemAlloc((strlenW(pszFileName
) + 1) * sizeof(WCHAR
));
597 strcpyW(This
->pszFileName
, pszFileName
);
599 This
->pmt
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
));
602 This
->pmt
->bFixedSizeSamples
= TRUE
;
603 This
->pmt
->bTemporalCompression
= FALSE
;
604 This
->pmt
->cbFormat
= 0;
605 This
->pmt
->pbFormat
= NULL
;
606 This
->pmt
->pUnk
= NULL
;
607 This
->pmt
->lSampleSize
= 0;
608 This
->pmt
->formattype
= FORMAT_None
;
609 hr
= GetClassMediaFile(pReader
, pszFileName
, &This
->pmt
->majortype
, &This
->pmt
->subtype
);
612 CoTaskMemFree(This
->pmt
);
617 CopyMediaType(This
->pmt
, pmt
);
621 IAsyncReader_Release(pReader
);
625 if (This
->pOutputPin
)
627 IPin_Release(This
->pOutputPin
);
628 This
->pOutputPin
= NULL
;
631 CoTaskMemFree(This
->pszFileName
);
633 FreeMediaType(This
->pmt
);
634 This
->pszFileName
= NULL
;
640 /* FIXME: check return codes */
644 static HRESULT WINAPI
FileSource_GetCurFile(IFileSourceFilter
* iface
, LPOLESTR
* ppszFileName
, AM_MEDIA_TYPE
* pmt
)
646 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
648 TRACE("(%p, %p)\n", ppszFileName
, pmt
);
653 /* copy file name & media type if available, otherwise clear the outputs */
654 if (This
->pszFileName
)
656 *ppszFileName
= CoTaskMemAlloc((strlenW(This
->pszFileName
) + 1) * sizeof(WCHAR
));
657 strcpyW(*ppszFileName
, This
->pszFileName
);
660 *ppszFileName
= NULL
;
665 CopyMediaType(pmt
, This
->pmt
);
667 ZeroMemory(pmt
, sizeof(*pmt
));
673 static const IFileSourceFilterVtbl FileSource_Vtbl
=
675 FileSource_QueryInterface
,
679 FileSource_GetCurFile
683 /* the dwUserData passed back to user */
684 typedef struct DATAREQUEST
686 IMediaSample
* pSample
; /* sample passed to us by user */
687 DWORD_PTR dwUserData
; /* user data passed to us */
688 OVERLAPPED ovl
; /* our overlapped structure */
691 typedef struct FileAsyncReader
694 IAsyncReader IAsyncReader_iface
;
696 ALLOCATOR_PROPERTIES allocProps
;
699 /* Why would you need more? Every sample has its own handle */
703 CRITICAL_SECTION csList
; /* critical section to prevent concurrency issues */
704 DATAREQUEST
*sample_list
;
706 /* Have a handle for every sample, and then one more as flushing handle */
710 static inline FileAsyncReader
*impl_from_IPin(IPin
*iface
)
712 return CONTAINING_RECORD(iface
, FileAsyncReader
, pin
.pin
.IPin_iface
);
715 static inline FileAsyncReader
*impl_from_BasePin(BasePin
*iface
)
717 return CONTAINING_RECORD(iface
, FileAsyncReader
, pin
.pin
);
720 static inline FileAsyncReader
*impl_from_BaseOutputPin(BaseOutputPin
*iface
)
722 return CONTAINING_RECORD(iface
, FileAsyncReader
, pin
);
725 static inline BaseOutputPin
*impl_BaseOututPin_from_BasePin(BasePin
*iface
)
727 return CONTAINING_RECORD(iface
, BaseOutputPin
, pin
);
730 static inline FileAsyncReader
*impl_from_IAsyncReader(IAsyncReader
*iface
)
732 return CONTAINING_RECORD(iface
, FileAsyncReader
, IAsyncReader_iface
);
735 static HRESULT WINAPI
FileAsyncReaderPin_QueryAccept(IPin
*iface
, const AM_MEDIA_TYPE
*pmt
)
737 FileAsyncReader
*This
= impl_from_IPin(iface
);
738 AM_MEDIA_TYPE
*pmt_filter
= impl_from_IBaseFilter(This
->pin
.pin
.pinInfo
.pFilter
)->pmt
;
740 FIXME("(%p, %p)\n", iface
, pmt
);
742 if (IsEqualGUID(&pmt
->majortype
, &pmt_filter
->majortype
) &&
743 IsEqualGUID(&pmt
->subtype
, &pmt_filter
->subtype
) &&
744 IsEqualGUID(&pmt
->formattype
, &FORMAT_None
))
750 static HRESULT WINAPI
FileAsyncReaderPin_GetMediaType(BasePin
*iface
, int iPosition
, AM_MEDIA_TYPE
*pmt
)
752 FileAsyncReader
*This
= impl_from_BasePin(iface
);
756 return VFW_S_NO_MORE_ITEMS
;
757 CopyMediaType(pmt
, impl_from_IBaseFilter(This
->pin
.pin
.pinInfo
.pFilter
)->pmt
);
761 /* overridden pin functions */
763 static HRESULT WINAPI
FileAsyncReaderPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
765 FileAsyncReader
*This
= impl_from_IPin(iface
);
766 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
770 if (IsEqualIID(riid
, &IID_IUnknown
))
772 else if (IsEqualIID(riid
, &IID_IPin
))
774 else if (IsEqualIID(riid
, &IID_IAsyncReader
))
775 *ppv
= &This
->IAsyncReader_iface
;
779 IUnknown_AddRef((IUnknown
*)(*ppv
));
783 if (!IsEqualIID(riid
, &IID_IPin
) && !IsEqualIID(riid
, &IID_IMediaSeeking
))
784 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
786 return E_NOINTERFACE
;
789 static ULONG WINAPI
FileAsyncReaderPin_Release(IPin
* iface
)
791 FileAsyncReader
*This
= impl_from_IPin(iface
);
792 ULONG refCount
= InterlockedDecrement(&This
->pin
.pin
.refCount
);
795 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
799 CoTaskMemFree(This
->sample_list
);
800 if (This
->handle_list
)
802 for (x
= 0; x
<= This
->samples
; ++x
)
803 CloseHandle(This
->handle_list
[x
]);
804 CoTaskMemFree(This
->handle_list
);
806 CloseHandle(This
->hFile
);
807 This
->csList
.DebugInfo
->Spare
[0] = 0;
808 DeleteCriticalSection(&This
->csList
);
815 static const IPinVtbl FileAsyncReaderPin_Vtbl
=
817 FileAsyncReaderPin_QueryInterface
,
819 FileAsyncReaderPin_Release
,
820 BaseOutputPinImpl_Connect
,
821 BaseOutputPinImpl_ReceiveConnection
,
822 BasePinImpl_Disconnect
,
823 BasePinImpl_ConnectedTo
,
824 BasePinImpl_ConnectionMediaType
,
825 BasePinImpl_QueryPinInfo
,
826 BasePinImpl_QueryDirection
,
828 FileAsyncReaderPin_QueryAccept
,
829 BasePinImpl_EnumMediaTypes
,
830 BasePinImpl_QueryInternalConnections
,
831 BaseOutputPinImpl_EndOfStream
,
832 BaseOutputPinImpl_BeginFlush
,
833 BaseOutputPinImpl_EndFlush
,
834 BasePinImpl_NewSegment
837 /* Function called as a helper to IPin_Connect */
838 /* specific AM_MEDIA_TYPE - it cannot be NULL */
839 /* this differs from standard OutputPin_AttemptConnection only in that it
840 * doesn't need the IMemInputPin interface on the receiving pin */
841 static HRESULT WINAPI
FileAsyncReaderPin_AttemptConnection(BasePin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
843 BaseOutputPin
*This
= impl_BaseOututPin_from_BasePin(iface
);
846 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
847 dump_AM_MEDIA_TYPE(pmt
);
849 /* FIXME: call queryacceptproc */
851 This
->pin
.pConnectedTo
= pReceivePin
;
852 IPin_AddRef(pReceivePin
);
853 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
855 hr
= IPin_ReceiveConnection(pReceivePin
, &iface
->IPin_iface
, pmt
);
859 IPin_Release(This
->pin
.pConnectedTo
);
860 This
->pin
.pConnectedTo
= NULL
;
861 FreeMediaType(&This
->pin
.mtCurrent
);
864 TRACE(" -- %x\n", hr
);
868 static HRESULT WINAPI
FileAsyncReaderPin_DecideBufferSize(BaseOutputPin
*iface
, IMemAllocator
*pAlloc
, ALLOCATOR_PROPERTIES
*ppropInputRequest
)
870 FileAsyncReader
*This
= impl_from_BaseOutputPin(iface
);
871 ALLOCATOR_PROPERTIES actual
;
873 if (ppropInputRequest
->cbAlign
&& ppropInputRequest
->cbAlign
!= This
->allocProps
.cbAlign
)
874 FIXME("Requested Buffer cbAlign mismatch %i,%i\n",This
->allocProps
.cbAlign
, ppropInputRequest
->cbAlign
);
875 if (ppropInputRequest
->cbPrefix
)
876 FIXME("Requested Buffer cbPrefix mismatch %i,%i\n",This
->allocProps
.cbPrefix
, ppropInputRequest
->cbPrefix
);
877 if (ppropInputRequest
->cbBuffer
)
878 FIXME("Requested Buffer cbBuffer mismatch %i,%i\n",This
->allocProps
.cbBuffer
, ppropInputRequest
->cbBuffer
);
879 if (ppropInputRequest
->cBuffers
)
880 FIXME("Requested Buffer cBuffers mismatch %i,%i\n",This
->allocProps
.cBuffers
, ppropInputRequest
->cBuffers
);
882 return IMemAllocator_SetProperties(pAlloc
, &This
->allocProps
, &actual
);
885 static const BasePinFuncTable output_BaseFuncTable
= {
887 FileAsyncReaderPin_AttemptConnection
,
888 BasePinImpl_GetMediaTypeVersion
,
889 FileAsyncReaderPin_GetMediaType
892 static const BaseOutputPinFuncTable output_BaseOutputFuncTable
= {
893 FileAsyncReaderPin_DecideBufferSize
,
894 BaseOutputPinImpl_DecideAllocator
,
895 BaseOutputPinImpl_BreakConnect
898 static HRESULT
FileAsyncReader_Construct(HANDLE hFile
, IBaseFilter
* pBaseFilter
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
904 piOutput
.dir
= PINDIR_OUTPUT
;
905 piOutput
.pFilter
= pBaseFilter
;
906 strcpyW(piOutput
.achName
, wszOutputPinName
);
907 hr
= BaseOutputPin_Construct(&FileAsyncReaderPin_Vtbl
, sizeof(FileAsyncReader
), &piOutput
, &output_BaseFuncTable
, &output_BaseOutputFuncTable
, pCritSec
, ppPin
);
911 FileAsyncReader
*pPinImpl
= (FileAsyncReader
*)*ppPin
;
912 pPinImpl
->IAsyncReader_iface
.lpVtbl
= &FileAsyncReader_Vtbl
;
913 pPinImpl
->hFile
= hFile
;
914 pPinImpl
->bFlushing
= FALSE
;
915 pPinImpl
->sample_list
= NULL
;
916 pPinImpl
->handle_list
= NULL
;
917 pPinImpl
->queued_number
= 0;
918 InitializeCriticalSection(&pPinImpl
->csList
);
919 pPinImpl
->csList
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FileAsyncReader.csList");
926 static HRESULT WINAPI
FileAsyncReader_QueryInterface(IAsyncReader
* iface
, REFIID riid
, LPVOID
* ppv
)
928 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
930 return IPin_QueryInterface(&This
->pin
.pin
.IPin_iface
, riid
, ppv
);
933 static ULONG WINAPI
FileAsyncReader_AddRef(IAsyncReader
* iface
)
935 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
937 return IPin_AddRef(&This
->pin
.pin
.IPin_iface
);
940 static ULONG WINAPI
FileAsyncReader_Release(IAsyncReader
* iface
)
942 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
944 return IPin_Release(&This
->pin
.pin
.IPin_iface
);
947 #define DEF_ALIGNMENT 1
949 static HRESULT WINAPI
FileAsyncReader_RequestAllocator(IAsyncReader
* iface
, IMemAllocator
* pPreferred
, ALLOCATOR_PROPERTIES
* pProps
, IMemAllocator
** ppActual
)
951 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
955 TRACE("(%p, %p, %p)\n", pPreferred
, pProps
, ppActual
);
957 if (!pProps
->cbAlign
|| (pProps
->cbAlign
% DEF_ALIGNMENT
) != 0)
958 pProps
->cbAlign
= DEF_ALIGNMENT
;
962 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, pProps
);
963 /* FIXME: check we are still aligned */
966 IMemAllocator_AddRef(pPreferred
);
967 *ppActual
= pPreferred
;
968 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr
);
975 hr
= CoCreateInstance(&CLSID_MemoryAllocator
, NULL
, CLSCTX_INPROC
, &IID_IMemAllocator
, (LPVOID
*)&pPreferred
);
979 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, pProps
);
980 /* FIXME: check we are still aligned */
983 *ppActual
= pPreferred
;
984 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr
);
991 CoTaskMemFree(This
->sample_list
);
992 if (This
->handle_list
)
995 for (x
= 0; x
<= This
->samples
; ++x
)
996 CloseHandle(This
->handle_list
[x
]);
997 CoTaskMemFree(This
->handle_list
);
1000 This
->samples
= pProps
->cBuffers
;
1001 This
->oldest_sample
= 0;
1002 TRACE("Samples: %u\n", This
->samples
);
1003 This
->sample_list
= CoTaskMemAlloc(sizeof(This
->sample_list
[0]) * pProps
->cBuffers
);
1004 This
->handle_list
= CoTaskMemAlloc(sizeof(HANDLE
) * pProps
->cBuffers
* 2);
1006 if (This
->sample_list
&& This
->handle_list
)
1009 ZeroMemory(This
->sample_list
, sizeof(This
->sample_list
[0]) * pProps
->cBuffers
);
1010 for (x
= 0; x
< This
->samples
; ++x
)
1012 This
->sample_list
[x
].ovl
.hEvent
= This
->handle_list
[x
] = CreateEventW(NULL
, 0, 0, NULL
);
1013 if (x
+ 1 < This
->samples
)
1014 This
->handle_list
[This
->samples
+ 1 + x
] = This
->handle_list
[x
];
1016 This
->handle_list
[This
->samples
] = CreateEventW(NULL
, 1, 0, NULL
);
1017 This
->allocProps
= *pProps
;
1022 CoTaskMemFree(This
->sample_list
);
1023 CoTaskMemFree(This
->handle_list
);
1025 This
->sample_list
= NULL
;
1026 This
->handle_list
= NULL
;
1034 IMemAllocator_Release(pPreferred
);
1037 TRACE("-- %x\n", hr
);
1041 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
1042 * however, this would be quite complicated to do and may be a bit error prone */
1043 static HRESULT WINAPI
FileAsyncReader_Request(IAsyncReader
* iface
, IMediaSample
* pSample
, DWORD_PTR dwUser
)
1046 REFERENCE_TIME Start
;
1047 REFERENCE_TIME Stop
;
1048 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1049 LPBYTE pBuffer
= NULL
;
1051 TRACE("(%p, %lx)\n", pSample
, dwUser
);
1056 /* get start and stop positions in bytes */
1058 hr
= IMediaSample_GetTime(pSample
, &Start
, &Stop
);
1061 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
1063 EnterCriticalSection(&This
->csList
);
1064 if (This
->bFlushing
)
1066 LeaveCriticalSection(&This
->csList
);
1067 return VFW_E_WRONG_STATE
;
1072 DWORD dwLength
= (DWORD
) BYTES_FROM_MEDIATIME(Stop
- Start
);
1073 DATAREQUEST
*pDataRq
;
1076 /* Try to insert above the waiting sample if possible */
1077 for (x
= This
->oldest_sample
; x
< This
->samples
; ++x
)
1079 if (!This
->sample_list
[x
].pSample
)
1083 if (x
>= This
->samples
)
1084 for (x
= 0; x
< This
->oldest_sample
; ++x
)
1086 if (!This
->sample_list
[x
].pSample
)
1090 /* There must be a sample we have found */
1091 assert(x
< This
->samples
);
1092 ++This
->queued_number
;
1094 pDataRq
= This
->sample_list
+ x
;
1096 pDataRq
->ovl
.u
.s
.Offset
= (DWORD
) BYTES_FROM_MEDIATIME(Start
);
1097 pDataRq
->ovl
.u
.s
.OffsetHigh
= (DWORD
)(BYTES_FROM_MEDIATIME(Start
) >> (sizeof(DWORD
) * 8));
1098 pDataRq
->dwUserData
= dwUser
;
1100 /* we violate traditional COM rules here by maintaining
1101 * a reference to the sample, but not calling AddRef, but
1102 * that's what MSDN says to do */
1103 pDataRq
->pSample
= pSample
;
1105 /* this is definitely not how it is implemented on Win9x
1106 * as they do not support async reads on files, but it is
1107 * sooo much easier to use this than messing around with threads!
1109 if (!ReadFile(This
->hFile
, pBuffer
, dwLength
, NULL
, &pDataRq
->ovl
))
1110 hr
= HRESULT_FROM_WIN32(GetLastError());
1112 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1113 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1117 LeaveCriticalSection(&This
->csList
);
1119 TRACE("-- %x\n", hr
);
1123 static HRESULT WINAPI
FileAsyncReader_WaitForNext(IAsyncReader
* iface
, DWORD dwTimeout
, IMediaSample
** ppSample
, DWORD_PTR
* pdwUser
)
1126 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1129 TRACE("(%u, %p, %p)\n", dwTimeout
, ppSample
, pdwUser
);
1134 EnterCriticalSection(&This
->csList
);
1135 if (!This
->bFlushing
)
1137 LONG oldest
= This
->oldest_sample
;
1139 if (!This
->queued_number
)
1141 /* It could be that nothing is queued right now, but that can be fixed */
1142 WARN("Called without samples in queue and not flushing!!\n");
1144 LeaveCriticalSection(&This
->csList
);
1146 /* wait for an object to read, or time out */
1147 buffer
= WaitForMultipleObjectsEx(This
->samples
+1, This
->handle_list
+ oldest
, FALSE
, dwTimeout
, TRUE
);
1149 EnterCriticalSection(&This
->csList
);
1150 if (buffer
<= This
->samples
)
1152 /* Re-scale the buffer back to normal */
1155 /* Uh oh, we overshot the flusher handle, renormalize it back to 0..Samples-1 */
1156 if (buffer
> This
->samples
)
1157 buffer
-= This
->samples
+ 1;
1158 assert(buffer
<= This
->samples
);
1161 if (buffer
>= This
->samples
)
1163 if (buffer
!= This
->samples
)
1165 FIXME("Returned: %u (%08x)\n", buffer
, GetLastError());
1169 hr
= VFW_E_WRONG_STATE
;
1173 --This
->queued_number
;
1176 if (This
->bFlushing
&& buffer
== ~0)
1178 for (buffer
= 0; buffer
< This
->samples
; ++buffer
)
1180 if (This
->sample_list
[buffer
].pSample
)
1182 ResetEvent(This
->handle_list
[buffer
]);
1186 if (buffer
== This
->samples
)
1188 assert(!This
->queued_number
);
1193 --This
->queued_number
;
1200 REFERENCE_TIME rtStart
, rtStop
;
1201 REFERENCE_TIME rtSampleStart
, rtSampleStop
;
1202 DATAREQUEST
*pDataRq
= This
->sample_list
+ buffer
;
1205 /* get any errors */
1206 if (!This
->bFlushing
&& !GetOverlappedResult(This
->hFile
, &pDataRq
->ovl
, &dwBytes
, FALSE
))
1207 hr
= HRESULT_FROM_WIN32(GetLastError());
1209 /* Return the sample no matter what so it can be destroyed */
1210 *ppSample
= pDataRq
->pSample
;
1211 *pdwUser
= pDataRq
->dwUserData
;
1213 if (This
->bFlushing
)
1214 hr
= VFW_E_WRONG_STATE
;
1219 /* Set the time on the sample */
1220 IMediaSample_SetActualDataLength(pDataRq
->pSample
, dwBytes
);
1222 rtStart
= (DWORD64
)pDataRq
->ovl
.u
.s
.Offset
+ ((DWORD64
)pDataRq
->ovl
.u
.s
.OffsetHigh
<< 32);
1223 rtStart
= MEDIATIME_FROM_BYTES(rtStart
);
1224 rtStop
= rtStart
+ MEDIATIME_FROM_BYTES(dwBytes
);
1226 IMediaSample_GetTime(pDataRq
->pSample
, &rtSampleStart
, &rtSampleStop
);
1227 assert(rtStart
== rtSampleStart
);
1228 assert(rtStop
<= rtSampleStop
);
1230 IMediaSample_SetTime(pDataRq
->pSample
, &rtStart
, &rtStop
);
1231 assert(rtStart
== rtSampleStart
);
1233 assert(rtStop
== rtSampleStop
);
1235 assert(rtStop
== rtStart
);
1237 This
->sample_list
[buffer
].pSample
= NULL
;
1238 assert(This
->oldest_sample
< This
->samples
);
1240 if (buffer
== This
->oldest_sample
)
1243 for (x
= This
->oldest_sample
+ 1; x
< This
->samples
; ++x
)
1244 if (This
->sample_list
[x
].pSample
)
1246 if (x
>= This
->samples
)
1247 for (x
= 0; x
< This
->oldest_sample
; ++x
)
1248 if (This
->sample_list
[x
].pSample
)
1250 if (This
->oldest_sample
== x
)
1251 /* No samples found, reset to 0 */
1253 This
->oldest_sample
= x
;
1256 LeaveCriticalSection(&This
->csList
);
1258 TRACE("-- %x\n", hr
);
1262 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
);
1264 static HRESULT WINAPI
FileAsyncReader_SyncReadAligned(IAsyncReader
* iface
, IMediaSample
* pSample
)
1267 REFERENCE_TIME tStart
;
1268 REFERENCE_TIME tStop
;
1271 TRACE("(%p)\n", pSample
);
1273 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
1276 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
1279 hr
= FileAsyncReader_SyncRead(iface
,
1280 BYTES_FROM_MEDIATIME(tStart
),
1281 (LONG
) BYTES_FROM_MEDIATIME(tStop
- tStart
),
1284 TRACE("-- %x\n", hr
);
1288 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
)
1292 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1294 TRACE("(%x%08x, %d, %p)\n", (ULONG
)(llPosition
>> 32), (ULONG
)llPosition
, lLength
, pBuffer
);
1296 ZeroMemory(&ovl
, sizeof(ovl
));
1298 ovl
.hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
1299 /* NOTE: llPosition is the actual byte position to start reading from */
1300 ovl
.u
.s
.Offset
= (DWORD
) llPosition
;
1301 ovl
.u
.s
.OffsetHigh
= (DWORD
) (llPosition
>> (sizeof(DWORD
) * 8));
1303 if (!ReadFile(This
->hFile
, pBuffer
, lLength
, NULL
, &ovl
))
1304 hr
= HRESULT_FROM_WIN32(GetLastError());
1306 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1313 if (!GetOverlappedResult(This
->hFile
, &ovl
, &dwBytesRead
, TRUE
))
1314 hr
= HRESULT_FROM_WIN32(GetLastError());
1317 CloseHandle(ovl
.hEvent
);
1319 TRACE("-- %x\n", hr
);
1323 static HRESULT WINAPI
FileAsyncReader_Length(IAsyncReader
* iface
, LONGLONG
* pTotal
, LONGLONG
* pAvailable
)
1327 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1329 TRACE("(%p, %p)\n", pTotal
, pAvailable
);
1331 if (((dwSizeLow
= GetFileSize(This
->hFile
, &dwSizeHigh
)) == -1) &&
1332 (GetLastError() != NO_ERROR
))
1333 return HRESULT_FROM_WIN32(GetLastError());
1335 *pTotal
= (LONGLONG
)dwSizeLow
| (LONGLONG
)dwSizeHigh
<< (sizeof(DWORD
) * 8);
1337 *pAvailable
= *pTotal
;
1342 static HRESULT WINAPI
FileAsyncReader_BeginFlush(IAsyncReader
* iface
)
1344 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1348 EnterCriticalSection(&This
->csList
);
1349 This
->bFlushing
= TRUE
;
1350 CancelIo(This
->hFile
);
1351 SetEvent(This
->handle_list
[This
->samples
]);
1352 LeaveCriticalSection(&This
->csList
);
1357 static HRESULT WINAPI
FileAsyncReader_EndFlush(IAsyncReader
* iface
)
1359 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1364 EnterCriticalSection(&This
->csList
);
1365 ResetEvent(This
->handle_list
[This
->samples
]);
1366 This
->bFlushing
= FALSE
;
1367 for (x
= 0; x
< This
->samples
; ++x
)
1368 assert(!This
->sample_list
[x
].pSample
);
1370 LeaveCriticalSection(&This
->csList
);
1375 static const IAsyncReaderVtbl FileAsyncReader_Vtbl
=
1377 FileAsyncReader_QueryInterface
,
1378 FileAsyncReader_AddRef
,
1379 FileAsyncReader_Release
,
1380 FileAsyncReader_RequestAllocator
,
1381 FileAsyncReader_Request
,
1382 FileAsyncReader_WaitForNext
,
1383 FileAsyncReader_SyncReadAligned
,
1384 FileAsyncReader_SyncRead
,
1385 FileAsyncReader_Length
,
1386 FileAsyncReader_BeginFlush
,
1387 FileAsyncReader_EndFlush
,
1391 static HRESULT WINAPI
AMFilterMiscFlags_QueryInterface(IAMFilterMiscFlags
*iface
, REFIID riid
, void **ppv
) {
1392 AsyncReader
*This
= impl_from_IAMFilterMiscFlags(iface
);
1393 return IUnknown_QueryInterface((IUnknown
*)This
, riid
, ppv
);
1396 static ULONG WINAPI
AMFilterMiscFlags_AddRef(IAMFilterMiscFlags
*iface
) {
1397 AsyncReader
*This
= impl_from_IAMFilterMiscFlags(iface
);
1398 return IUnknown_AddRef((IUnknown
*)This
);
1401 static ULONG WINAPI
AMFilterMiscFlags_Release(IAMFilterMiscFlags
*iface
) {
1402 AsyncReader
*This
= impl_from_IAMFilterMiscFlags(iface
);
1403 return IUnknown_Release((IUnknown
*)This
);
1406 static ULONG WINAPI
AMFilterMiscFlags_GetMiscFlags(IAMFilterMiscFlags
*iface
) {
1407 return AM_FILTER_MISC_FLAGS_IS_SOURCE
;
1410 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl
= {
1411 AMFilterMiscFlags_QueryInterface
,
1412 AMFilterMiscFlags_AddRef
,
1413 AMFilterMiscFlags_Release
,
1414 AMFilterMiscFlags_GetMiscFlags