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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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
42 const IBaseFilterVtbl
* lpVtbl
;
43 const IFileSourceFilterVtbl
* lpVtblFSF
;
46 FILTER_INFO filterInfo
;
48 CRITICAL_SECTION csFilter
;
55 static const IBaseFilterVtbl AsyncReader_Vtbl
;
56 static const IFileSourceFilterVtbl FileSource_Vtbl
;
57 static const IAsyncReaderVtbl FileAsyncReader_Vtbl
;
59 static HRESULT
FileAsyncReader_Construct(HANDLE hFile
, IBaseFilter
* pBaseFilter
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
);
61 static inline AsyncReader
*impl_from_IFileSourceFilter( IFileSourceFilter
*iface
)
63 return (AsyncReader
*)((char*)iface
- FIELD_OFFSET(AsyncReader
, lpVtblFSF
));
66 static HRESULT
process_extensions(HKEY hkeyExtensions
, LPCOLESTR pszFileName
, GUID
* majorType
, GUID
* minorType
)
68 /* FIXME: implement */
72 static unsigned char byte_from_hex_char(WCHAR wHex
)
74 switch (tolowerW(wHex
))
93 return wHex
- 'a' + 10;
99 static HRESULT
process_pattern_string(LPCWSTR wszPatternString
, IAsyncReader
* pReader
)
109 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString
));
111 /* format: "offset, bytestocompare, mask, value" */
113 ulOffset
= strtolW(wszPatternString
, NULL
, 10);
115 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
118 wszPatternString
++; /* skip ',' */
120 ulBytes
= strtolW(wszPatternString
, NULL
, 10);
122 pbMask
= HeapAlloc(GetProcessHeap(), 0, ulBytes
);
123 pbValue
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, ulBytes
);
124 pbFile
= HeapAlloc(GetProcessHeap(), 0, ulBytes
);
126 /* default mask is match everything */
127 memset(pbMask
, 0xFF, ulBytes
);
129 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
132 wszPatternString
++; /* skip ',' */
136 for ( ; !isxdigitW(*wszPatternString
) && (*wszPatternString
!= ','); wszPatternString
++)
139 for (strpos
= 0; isxdigitW(*wszPatternString
) && (strpos
/2 < ulBytes
); wszPatternString
++, strpos
++)
141 if ((strpos
% 2) == 1) /* odd numbered position */
142 pbMask
[strpos
/ 2] |= byte_from_hex_char(*wszPatternString
);
144 pbMask
[strpos
/ 2] = byte_from_hex_char(*wszPatternString
) << 4;
147 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
150 wszPatternString
++; /* skip ',' */
155 for ( ; !isxdigitW(*wszPatternString
) && (*wszPatternString
!= ','); wszPatternString
++)
158 for (strpos
= 0; isxdigitW(*wszPatternString
) && (strpos
/2 < ulBytes
); wszPatternString
++, strpos
++)
160 if ((strpos
% 2) == 1) /* odd numbered position */
161 pbValue
[strpos
/ 2] |= byte_from_hex_char(*wszPatternString
);
163 pbValue
[strpos
/ 2] = byte_from_hex_char(*wszPatternString
) << 4;
168 hr
= IAsyncReader_SyncRead(pReader
, ulOffset
, ulBytes
, pbFile
);
173 for (i
= 0; i
< ulBytes
; i
++)
174 if ((pbFile
[i
] & pbMask
[i
]) != pbValue
[i
])
181 HeapFree(GetProcessHeap(), 0, pbMask
);
182 HeapFree(GetProcessHeap(), 0, pbValue
);
183 HeapFree(GetProcessHeap(), 0, pbFile
);
185 /* if we encountered no errors with this string, and there is a following tuple, then we
186 * have to match that as well to succeed */
187 if ((hr
== S_OK
) && (wszPatternString
= strchrW(wszPatternString
, ',')))
188 return process_pattern_string(wszPatternString
+ 1, pReader
);
193 static HRESULT
GetClassMediaFile(IAsyncReader
* pReader
, LPCOLESTR pszFileName
, GUID
* majorType
, GUID
* minorType
)
195 HKEY hkeyMediaType
= NULL
;
198 static const WCHAR wszMediaType
[] = {'M','e','d','i','a',' ','T','y','p','e',0};
200 CopyMemory(majorType
, &GUID_NULL
, sizeof(*majorType
));
201 CopyMemory(minorType
, &GUID_NULL
, sizeof(*minorType
));
203 hr
= HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_CLASSES_ROOT
, wszMediaType
, 0, KEY_READ
, &hkeyMediaType
));
209 for (indexMajor
= 0; !bFound
; indexMajor
++)
212 WCHAR wszMajorKeyName
[CHARS_IN_GUID
];
213 DWORD dwKeyNameLength
= sizeof(wszMajorKeyName
) / sizeof(wszMajorKeyName
[0]);
214 static const WCHAR wszExtensions
[] = {'E','x','t','e','n','s','i','o','n','s',0};
216 if (RegEnumKeyExW(hkeyMediaType
, indexMajor
, wszMajorKeyName
, &dwKeyNameLength
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
218 if (RegOpenKeyExW(hkeyMediaType
, wszMajorKeyName
, 0, KEY_READ
, &hkeyMajor
) != ERROR_SUCCESS
)
220 TRACE("%s\n", debugstr_w(wszMajorKeyName
));
221 if (!strcmpW(wszExtensions
, wszMajorKeyName
))
223 if (process_extensions(hkeyMajor
, pszFileName
, majorType
, minorType
) == S_OK
)
230 for (indexMinor
= 0; !bFound
; indexMinor
++)
233 WCHAR wszMinorKeyName
[CHARS_IN_GUID
];
234 DWORD dwMinorKeyNameLen
= sizeof(wszMinorKeyName
) / sizeof(wszMinorKeyName
[0]);
238 if (RegEnumKeyExW(hkeyMajor
, indexMinor
, wszMinorKeyName
, &dwMinorKeyNameLen
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
241 if (RegOpenKeyExW(hkeyMajor
, wszMinorKeyName
, 0, KEY_READ
, &hkeyMinor
) != ERROR_SUCCESS
)
244 TRACE("\t%s\n", debugstr_w(wszMinorKeyName
));
246 if (RegQueryInfoKeyW(hkeyMinor
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &maxValueLen
, NULL
, NULL
) != ERROR_SUCCESS
)
249 for (indexValue
= 0; !bFound
; indexValue
++)
252 WCHAR wszValueName
[14]; /* longest name we should encounter will be "Source Filter" */
253 LPWSTR wszPatternString
= HeapAlloc(GetProcessHeap(), 0, maxValueLen
);
254 DWORD dwValueNameLen
= sizeof(wszValueName
) / sizeof(wszValueName
[0]); /* remember this is in chars */
255 DWORD dwDataLen
= maxValueLen
; /* remember this is in bytes */
256 static const WCHAR wszSourceFilter
[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
259 if ((temp
= RegEnumValueW(hkeyMinor
, indexValue
, wszValueName
, &dwValueNameLen
, NULL
, &dwType
, (LPBYTE
)wszPatternString
, &dwDataLen
)) != ERROR_SUCCESS
)
261 HeapFree(GetProcessHeap(), 0, wszPatternString
);
265 /* if it is not the source filter value */
266 if (strcmpW(wszValueName
, wszSourceFilter
))
268 if (process_pattern_string(wszPatternString
, pReader
) == S_OK
)
270 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName
, majorType
)) &&
271 SUCCEEDED(CLSIDFromString(wszMinorKeyName
, minorType
)))
275 HeapFree(GetProcessHeap(), 0, wszPatternString
);
277 CloseHandle(hkeyMinor
);
280 CloseHandle(hkeyMajor
);
283 CloseHandle(hkeyMediaType
);
285 if (SUCCEEDED(hr
) && !bFound
)
287 ERR("Media class not found\n");
291 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType
), qzdebugstr_guid(minorType
));
296 HRESULT
AsyncReader_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
298 AsyncReader
*pAsyncRead
;
301 return CLASS_E_NOAGGREGATION
;
303 pAsyncRead
= CoTaskMemAlloc(sizeof(AsyncReader
));
306 return E_OUTOFMEMORY
;
308 pAsyncRead
->lpVtbl
= &AsyncReader_Vtbl
;
309 pAsyncRead
->lpVtblFSF
= &FileSource_Vtbl
;
310 pAsyncRead
->refCount
= 1;
311 pAsyncRead
->filterInfo
.achName
[0] = '\0';
312 pAsyncRead
->filterInfo
.pGraph
= NULL
;
313 pAsyncRead
->pOutputPin
= NULL
;
315 InitializeCriticalSection(&pAsyncRead
->csFilter
);
317 pAsyncRead
->pszFileName
= NULL
;
318 pAsyncRead
->pmt
= NULL
;
320 *ppv
= (LPVOID
)pAsyncRead
;
322 TRACE("-- created at %p\n", pAsyncRead
);
327 /** IUnkown methods **/
329 static HRESULT WINAPI
AsyncReader_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
331 AsyncReader
*This
= (AsyncReader
*)iface
;
333 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
337 if (IsEqualIID(riid
, &IID_IUnknown
))
339 else if (IsEqualIID(riid
, &IID_IPersist
))
341 else if (IsEqualIID(riid
, &IID_IMediaFilter
))
343 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
345 else if (IsEqualIID(riid
, &IID_IFileSourceFilter
))
346 *ppv
= (LPVOID
)(&This
->lpVtblFSF
);
350 IUnknown_AddRef((IUnknown
*)(*ppv
));
354 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
356 return E_NOINTERFACE
;
359 static ULONG WINAPI
AsyncReader_AddRef(IBaseFilter
* iface
)
361 AsyncReader
*This
= (AsyncReader
*)iface
;
362 ULONG refCount
= InterlockedIncrement(&This
->refCount
);
364 TRACE("(%p/%p)->() AddRef from %ld\n", This
, iface
, refCount
- 1);
369 static ULONG WINAPI
AsyncReader_Release(IBaseFilter
* iface
)
371 AsyncReader
*This
= (AsyncReader
*)iface
;
372 ULONG refCount
= InterlockedDecrement(&This
->refCount
);
374 TRACE("(%p/%p)->() Release from %ld\n", This
, iface
, refCount
+ 1);
378 if (This
->pOutputPin
)
379 IPin_Release(This
->pOutputPin
);
380 DeleteCriticalSection(&This
->csFilter
);
389 /** IPersist methods **/
391 static HRESULT WINAPI
AsyncReader_GetClassID(IBaseFilter
* iface
, CLSID
* pClsid
)
393 TRACE("(%p)\n", pClsid
);
395 *pClsid
= CLSID_AsyncReader
;
400 /** IMediaFilter methods **/
402 static HRESULT WINAPI
AsyncReader_Stop(IBaseFilter
* iface
)
404 AsyncReader
*This
= (AsyncReader
*)iface
;
408 This
->state
= State_Stopped
;
413 static HRESULT WINAPI
AsyncReader_Pause(IBaseFilter
* iface
)
415 AsyncReader
*This
= (AsyncReader
*)iface
;
419 This
->state
= State_Paused
;
424 static HRESULT WINAPI
AsyncReader_Run(IBaseFilter
* iface
, REFERENCE_TIME tStart
)
426 AsyncReader
*This
= (AsyncReader
*)iface
;
428 TRACE("(%lx%08lx)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
);
430 This
->state
= State_Running
;
435 static HRESULT WINAPI
AsyncReader_GetState(IBaseFilter
* iface
, DWORD dwMilliSecsTimeout
, FILTER_STATE
*pState
)
437 AsyncReader
*This
= (AsyncReader
*)iface
;
439 TRACE("(%lu, %p)\n", dwMilliSecsTimeout
, pState
);
441 *pState
= This
->state
;
446 static HRESULT WINAPI
AsyncReader_SetSyncSource(IBaseFilter
* iface
, IReferenceClock
*pClock
)
448 /* AsyncReader *This = (AsyncReader *)iface;*/
450 TRACE("(%p)\n", pClock
);
455 static HRESULT WINAPI
AsyncReader_GetSyncSource(IBaseFilter
* iface
, IReferenceClock
**ppClock
)
457 /* AsyncReader *This = (AsyncReader *)iface;*/
459 TRACE("(%p)\n", ppClock
);
464 /** IBaseFilter methods **/
466 static HRESULT WINAPI
AsyncReader_EnumPins(IBaseFilter
* iface
, IEnumPins
**ppEnum
)
469 AsyncReader
*This
= (AsyncReader
*)iface
;
471 TRACE("(%p)\n", ppEnum
);
473 epd
.cPins
= This
->pOutputPin
? 1 : 0;
474 epd
.ppPins
= &This
->pOutputPin
;
475 return IEnumPinsImpl_Construct(&epd
, ppEnum
);
478 static HRESULT WINAPI
AsyncReader_FindPin(IBaseFilter
* iface
, LPCWSTR Id
, IPin
**ppPin
)
480 FIXME("(%s, %p)\n", debugstr_w(Id
), ppPin
);
485 static HRESULT WINAPI
AsyncReader_QueryFilterInfo(IBaseFilter
* iface
, FILTER_INFO
*pInfo
)
487 AsyncReader
*This
= (AsyncReader
*)iface
;
489 TRACE("(%p)\n", pInfo
);
491 strcpyW(pInfo
->achName
, This
->filterInfo
.achName
);
492 pInfo
->pGraph
= This
->filterInfo
.pGraph
;
495 IFilterGraph_AddRef(pInfo
->pGraph
);
500 static HRESULT WINAPI
AsyncReader_JoinFilterGraph(IBaseFilter
* iface
, IFilterGraph
*pGraph
, LPCWSTR pName
)
502 AsyncReader
*This
= (AsyncReader
*)iface
;
504 TRACE("(%p, %s)\n", pGraph
, debugstr_w(pName
));
507 strcpyW(This
->filterInfo
.achName
, pName
);
509 *This
->filterInfo
.achName
= 0;
510 This
->filterInfo
.pGraph
= pGraph
; /* NOTE: do NOT increase ref. count */
515 static HRESULT WINAPI
AsyncReader_QueryVendorInfo(IBaseFilter
* iface
, LPWSTR
*pVendorInfo
)
517 FIXME("(%p)\n", pVendorInfo
);
522 static const IBaseFilterVtbl AsyncReader_Vtbl
=
524 AsyncReader_QueryInterface
,
527 AsyncReader_GetClassID
,
531 AsyncReader_GetState
,
532 AsyncReader_SetSyncSource
,
533 AsyncReader_GetSyncSource
,
534 AsyncReader_EnumPins
,
536 AsyncReader_QueryFilterInfo
,
537 AsyncReader_JoinFilterGraph
,
538 AsyncReader_QueryVendorInfo
541 static HRESULT WINAPI
FileSource_QueryInterface(IFileSourceFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
543 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
545 return IBaseFilter_QueryInterface((IFileSourceFilter
*)&This
->lpVtbl
, riid
, ppv
);
548 static ULONG WINAPI
FileSource_AddRef(IFileSourceFilter
* iface
)
550 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
552 return IBaseFilter_AddRef((IFileSourceFilter
*)&This
->lpVtbl
);
555 static ULONG WINAPI
FileSource_Release(IFileSourceFilter
* iface
)
557 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
559 return IBaseFilter_Release((IFileSourceFilter
*)&This
->lpVtbl
);
562 static HRESULT WINAPI
FileSource_Load(IFileSourceFilter
* iface
, LPCOLESTR pszFileName
, const AM_MEDIA_TYPE
* pmt
)
566 IAsyncReader
* pReader
= NULL
;
567 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
569 TRACE("(%s, %p)\n", debugstr_w(pszFileName
), pmt
);
572 /* FIXME: check the sharing values that native uses */
573 hFile
= CreateFileW(pszFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, NULL
);
575 if (hFile
== INVALID_HANDLE_VALUE
)
577 return HRESULT_FROM_WIN32(GetLastError());
581 hr
= FileAsyncReader_Construct(hFile
, (IBaseFilter
*)&This
->lpVtbl
, &This
->csFilter
, &This
->pOutputPin
);
584 hr
= IPin_QueryInterface(This
->pOutputPin
, &IID_IAsyncReader
, (LPVOID
*)&pReader
);
586 /* store file name & media type */
589 This
->pszFileName
= CoTaskMemAlloc((strlenW(pszFileName
) + 1) * sizeof(WCHAR
));
590 strcpyW(This
->pszFileName
, pszFileName
);
591 This
->pmt
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
));
594 This
->pmt
->bFixedSizeSamples
= TRUE
;
595 This
->pmt
->bTemporalCompression
= FALSE
;
596 This
->pmt
->cbFormat
= 0;
597 This
->pmt
->pbFormat
= NULL
;
598 This
->pmt
->pUnk
= NULL
;
599 This
->pmt
->lSampleSize
= 0;
600 memcpy(&This
->pmt
->formattype
, &FORMAT_None
, sizeof(FORMAT_None
));
601 hr
= GetClassMediaFile(pReader
, pszFileName
, &This
->pmt
->majortype
, &This
->pmt
->subtype
);
604 CoTaskMemFree(This
->pmt
);
609 CopyMediaType(This
->pmt
, pmt
);
613 IAsyncReader_Release(pReader
);
617 if (This
->pOutputPin
)
619 IPin_Release(This
->pOutputPin
);
620 This
->pOutputPin
= NULL
;
622 if (This
->pszFileName
)
624 CoTaskMemFree(This
->pszFileName
);
625 This
->pszFileName
= NULL
;
630 /* FIXME: check return codes */
634 static HRESULT WINAPI
FileSource_GetCurFile(IFileSourceFilter
* iface
, LPOLESTR
* ppszFileName
, AM_MEDIA_TYPE
* pmt
)
636 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
638 TRACE("(%p, %p)\n", ppszFileName
, pmt
);
640 /* copy file name & media type if available, otherwise clear the outputs */
641 if (This
->pszFileName
)
643 *ppszFileName
= CoTaskMemAlloc((strlenW(This
->pszFileName
) + 1) * sizeof(WCHAR
));
644 strcpyW(*ppszFileName
, This
->pszFileName
);
647 *ppszFileName
= NULL
;
651 CopyMediaType(pmt
, This
->pmt
);
654 ZeroMemory(pmt
, sizeof(*pmt
));
659 static const IFileSourceFilterVtbl FileSource_Vtbl
=
661 FileSource_QueryInterface
,
665 FileSource_GetCurFile
669 /* the dwUserData passed back to user */
670 typedef struct DATAREQUEST
672 IMediaSample
* pSample
; /* sample passed to us by user */
673 DWORD_PTR dwUserData
; /* user data passed to us */
674 OVERLAPPED ovl
; /* our overlapped structure */
676 struct DATAREQUEST
* pNext
; /* next data request in list */
679 static void queue(DATAREQUEST
* pHead
, DATAREQUEST
* pItem
)
681 DATAREQUEST
* pCurrent
;
682 for (pCurrent
= pHead
; pCurrent
->pNext
; pCurrent
= pCurrent
->pNext
)
684 pCurrent
->pNext
= pItem
;
687 typedef struct FileAsyncReader
690 const struct IAsyncReaderVtbl
* lpVtblAR
;
695 DATAREQUEST
* pHead
; /* head of data request list */
696 CRITICAL_SECTION csList
; /* critical section to protect operations on list */
699 static inline FileAsyncReader
*impl_from_IAsyncReader( IAsyncReader
*iface
)
701 return (FileAsyncReader
*)((char*)iface
- FIELD_OFFSET(FileAsyncReader
, lpVtblAR
));
704 static HRESULT
AcceptProcAFR(LPVOID iface
, const AM_MEDIA_TYPE
*pmt
)
706 AsyncReader
*This
= (AsyncReader
*)iface
;
708 FIXME("(%p, %p)\n", iface
, pmt
);
710 if (IsEqualGUID(&pmt
->majortype
, &This
->pmt
->majortype
) &&
711 IsEqualGUID(&pmt
->subtype
, &This
->pmt
->subtype
) &&
712 IsEqualGUID(&pmt
->formattype
, &FORMAT_None
))
718 /* overriden pin functions */
720 static HRESULT WINAPI
FileAsyncReaderPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
722 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
723 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
727 if (IsEqualIID(riid
, &IID_IUnknown
))
729 else if (IsEqualIID(riid
, &IID_IPin
))
731 else if (IsEqualIID(riid
, &IID_IAsyncReader
))
732 *ppv
= (LPVOID
)&This
->lpVtblAR
;
736 IUnknown_AddRef((IUnknown
*)(*ppv
));
740 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
742 return E_NOINTERFACE
;
745 static ULONG WINAPI
FileAsyncReaderPin_Release(IPin
* iface
)
747 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
748 ULONG refCount
= InterlockedDecrement(&This
->pin
.pin
.refCount
);
754 DATAREQUEST
* pCurrent
;
756 for (pCurrent
= This
->pHead
; pCurrent
; pCurrent
= pNext
)
758 pNext
= pCurrent
->pNext
;
759 CoTaskMemFree(pCurrent
);
761 CloseHandle(This
->hFile
);
762 CloseHandle(This
->hEvent
);
769 static HRESULT WINAPI
FileAsyncReaderPin_EnumMediaTypes(IPin
* iface
, IEnumMediaTypes
** ppEnum
)
771 ENUMMEDIADETAILS emd
;
772 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
774 TRACE("(%p)\n", ppEnum
);
777 emd
.pMediaTypes
= ((AsyncReader
*)This
->pin
.pin
.pinInfo
.pFilter
)->pmt
;
779 return IEnumMediaTypesImpl_Construct(&emd
, ppEnum
);
782 static const IPinVtbl FileAsyncReaderPin_Vtbl
=
784 FileAsyncReaderPin_QueryInterface
,
786 FileAsyncReaderPin_Release
,
788 OutputPin_ReceiveConnection
,
790 IPinImpl_ConnectedTo
,
791 IPinImpl_ConnectionMediaType
,
792 IPinImpl_QueryPinInfo
,
793 IPinImpl_QueryDirection
,
795 IPinImpl_QueryAccept
,
796 FileAsyncReaderPin_EnumMediaTypes
,
797 IPinImpl_QueryInternalConnections
,
798 OutputPin_EndOfStream
,
799 OutputPin_BeginFlush
,
804 /* Function called as a helper to IPin_Connect */
805 /* specific AM_MEDIA_TYPE - it cannot be NULL */
806 /* this differs from standard OutputPin_ConnectSpecific only in that it
807 * doesn't need the IMemInputPin interface on the receiving pin */
808 static HRESULT
FileAsyncReaderPin_ConnectSpecific(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
810 OutputPin
*This
= (OutputPin
*)iface
;
813 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
814 dump_AM_MEDIA_TYPE(pmt
);
816 /* FIXME: call queryacceptproc */
818 This
->pin
.pConnectedTo
= pReceivePin
;
819 IPin_AddRef(pReceivePin
);
820 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
822 hr
= IPin_ReceiveConnection(pReceivePin
, iface
, pmt
);
826 IPin_Release(This
->pin
.pConnectedTo
);
827 This
->pin
.pConnectedTo
= NULL
;
828 FreeMediaType(&This
->pin
.mtCurrent
);
831 TRACE(" -- %lx\n", hr
);
835 static HRESULT
FileAsyncReader_Construct(HANDLE hFile
, IBaseFilter
* pBaseFilter
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
837 FileAsyncReader
* pPinImpl
;
842 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
845 return E_OUTOFMEMORY
;
847 piOutput
.dir
= PINDIR_OUTPUT
;
848 piOutput
.pFilter
= pBaseFilter
;
849 strcpyW(piOutput
.achName
, wszOutputPinName
);
851 if (SUCCEEDED(OutputPin_Init(&piOutput
, NULL
, pBaseFilter
, AcceptProcAFR
, pCritSec
, &pPinImpl
->pin
)))
853 pPinImpl
->pin
.pin
.lpVtbl
= &FileAsyncReaderPin_Vtbl
;
854 pPinImpl
->lpVtblAR
= &FileAsyncReader_Vtbl
;
855 pPinImpl
->hFile
= hFile
;
856 pPinImpl
->hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
857 pPinImpl
->bFlushing
= FALSE
;
858 pPinImpl
->pHead
= NULL
;
859 pPinImpl
->pin
.pConnectSpecific
= FileAsyncReaderPin_ConnectSpecific
;
860 InitializeCriticalSection(&pPinImpl
->csList
);
862 *ppPin
= (IPin
*)(&pPinImpl
->pin
.pin
.lpVtbl
);
870 static HRESULT WINAPI
FileAsyncReader_QueryInterface(IAsyncReader
* iface
, REFIID riid
, LPVOID
* ppv
)
872 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
874 return IPin_QueryInterface((IPin
*)This
, riid
, ppv
);
877 static ULONG WINAPI
FileAsyncReader_AddRef(IAsyncReader
* iface
)
879 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
881 return IPin_AddRef((IPin
*)This
);
884 static ULONG WINAPI
FileAsyncReader_Release(IAsyncReader
* iface
)
886 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
888 return IPin_Release((IPin
*)This
);
891 #define DEF_ALIGNMENT 1
893 static HRESULT WINAPI
FileAsyncReader_RequestAllocator(IAsyncReader
* iface
, IMemAllocator
* pPreferred
, ALLOCATOR_PROPERTIES
* pProps
, IMemAllocator
** ppActual
)
897 TRACE("(%p, %p, %p)\n", pPreferred
, pProps
, ppActual
);
899 if (!pProps
->cbAlign
|| (pProps
->cbAlign
% DEF_ALIGNMENT
) != 0)
900 pProps
->cbAlign
= DEF_ALIGNMENT
;
904 ALLOCATOR_PROPERTIES PropsActual
;
905 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, &PropsActual
);
906 /* FIXME: check we are still aligned */
909 IMemAllocator_AddRef(pPreferred
);
910 *ppActual
= pPreferred
;
911 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr
);
918 hr
= CoCreateInstance(&CLSID_MemoryAllocator
, NULL
, CLSCTX_INPROC
, &IID_IMemAllocator
, (LPVOID
*)&pPreferred
);
922 ALLOCATOR_PROPERTIES PropsActual
;
923 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, &PropsActual
);
924 /* FIXME: check we are still aligned */
927 IMemAllocator_AddRef(pPreferred
);
928 *ppActual
= pPreferred
;
929 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr
);
938 IMemAllocator_Release(pPreferred
);
941 TRACE("-- %lx\n", hr
);
945 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
946 * however, this would be quite complicated to do and may be a bit error prone */
947 static HRESULT WINAPI
FileAsyncReader_Request(IAsyncReader
* iface
, IMediaSample
* pSample
, DWORD_PTR dwUser
)
949 REFERENCE_TIME Start
;
951 DATAREQUEST
* pDataRq
;
954 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
956 TRACE("(%p, %lx)\n", pSample
, dwUser
);
958 /* check flushing state */
960 return VFW_E_WRONG_STATE
;
962 if (!(pDataRq
= CoTaskMemAlloc(sizeof(*pDataRq
))))
965 /* get start and stop positions in bytes */
967 hr
= IMediaSample_GetTime(pSample
, &Start
, &Stop
);
970 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
974 DWORD dwLength
= (DWORD
) BYTES_FROM_MEDIATIME(Stop
- Start
);
976 pDataRq
->ovl
.u
.s
.Offset
= (DWORD
) BYTES_FROM_MEDIATIME(Start
);
977 pDataRq
->ovl
.u
.s
.OffsetHigh
= (DWORD
)(BYTES_FROM_MEDIATIME(Start
) >> (sizeof(DWORD
) * 8));
978 pDataRq
->ovl
.hEvent
= This
->hEvent
;
979 pDataRq
->dwUserData
= dwUser
;
980 pDataRq
->pNext
= NULL
;
981 /* we violate traditional COM rules here by maintaining
982 * a reference to the sample, but not calling AddRef, but
983 * that's what MSDN says to do */
984 pDataRq
->pSample
= pSample
;
986 EnterCriticalSection(&This
->csList
);
989 /* adds data request to end of list */
990 queue(This
->pHead
, pDataRq
);
992 This
->pHead
= pDataRq
;
994 LeaveCriticalSection(&This
->csList
);
996 /* this is definitely not how it is implemented on Win9x
997 * as they do not support async reads on files, but it is
998 * sooo much easier to use this than messing around with threads!
1000 if (!ReadFile(This
->hFile
, pBuffer
, dwLength
, NULL
, &pDataRq
->ovl
))
1001 hr
= HRESULT_FROM_WIN32(GetLastError());
1003 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1004 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1008 if (FAILED(hr
) && pDataRq
)
1010 EnterCriticalSection(&This
->csList
);
1012 DATAREQUEST
* pCurrent
;
1013 for (pCurrent
= This
->pHead
; pCurrent
&& pCurrent
->pNext
; pCurrent
= pCurrent
->pNext
)
1014 if (pCurrent
->pNext
== pDataRq
)
1016 pCurrent
->pNext
= pDataRq
->pNext
;
1020 LeaveCriticalSection(&This
->csList
);
1021 CoTaskMemFree(pDataRq
);
1024 TRACE("-- %lx\n", hr
);
1028 static HRESULT WINAPI
FileAsyncReader_WaitForNext(IAsyncReader
* iface
, DWORD dwTimeout
, IMediaSample
** ppSample
, DWORD_PTR
* pdwUser
)
1031 DATAREQUEST
* pDataRq
= NULL
;
1032 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1034 TRACE("(%lu, %p, %p)\n", dwTimeout
, ppSample
, pdwUser
);
1036 /* FIXME: we could do with improving this by waiting for an array of event handles
1037 * and then determining which one finished and removing that from the list, otherwise
1038 * we will end up waiting for longer than we should do, if a later request finishes
1039 * before an earlier one */
1044 /* we return immediately if flushing */
1045 if (This
->bFlushing
)
1046 hr
= VFW_E_WRONG_STATE
;
1050 /* wait for the read to finish or timeout */
1051 if (WaitForSingleObject(This
->hEvent
, dwTimeout
) == WAIT_TIMEOUT
)
1056 EnterCriticalSection(&This
->csList
);
1058 pDataRq
= This
->pHead
;
1059 if (pDataRq
== NULL
)
1062 This
->pHead
= pDataRq
->pNext
;
1064 LeaveCriticalSection(&This
->csList
);
1070 /* get any errors */
1071 if (!GetOverlappedResult(This
->hFile
, &pDataRq
->ovl
, &dwBytes
, FALSE
))
1072 hr
= HRESULT_FROM_WIN32(GetLastError());
1077 *ppSample
= pDataRq
->pSample
;
1078 *pdwUser
= pDataRq
->dwUserData
;
1084 /* no need to close event handle since we will close it when the pin is destroyed */
1085 CoTaskMemFree(pDataRq
);
1088 TRACE("-- %lx\n", hr
);
1092 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
);
1094 static HRESULT WINAPI
FileAsyncReader_SyncReadAligned(IAsyncReader
* iface
, IMediaSample
* pSample
)
1097 REFERENCE_TIME tStart
;
1098 REFERENCE_TIME tStop
;
1101 TRACE("(%p)\n", pSample
);
1103 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
1106 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
1109 hr
= FileAsyncReader_SyncRead(iface
,
1110 BYTES_FROM_MEDIATIME(tStart
),
1111 (LONG
) BYTES_FROM_MEDIATIME(tStop
- tStart
),
1114 TRACE("-- %lx\n", hr
);
1118 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
)
1122 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1124 TRACE("(%lx%08lx, %ld, %p)\n", (ULONG
)(llPosition
>> 32), (ULONG
)llPosition
, lLength
, pBuffer
);
1126 ZeroMemory(&ovl
, sizeof(ovl
));
1128 ovl
.hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
1129 /* NOTE: llPosition is the actual byte position to start reading from */
1130 ovl
.u
.s
.Offset
= (DWORD
) llPosition
;
1131 ovl
.u
.s
.OffsetHigh
= (DWORD
) (llPosition
>> (sizeof(DWORD
) * 8));
1133 if (!ReadFile(This
->hFile
, pBuffer
, lLength
, NULL
, &ovl
))
1134 hr
= HRESULT_FROM_WIN32(GetLastError());
1136 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1143 if (!GetOverlappedResult(This
->hFile
, &ovl
, &dwBytesRead
, TRUE
))
1144 hr
= HRESULT_FROM_WIN32(GetLastError());
1147 CloseHandle(ovl
.hEvent
);
1149 TRACE("-- %lx\n", hr
);
1153 static HRESULT WINAPI
FileAsyncReader_Length(IAsyncReader
* iface
, LONGLONG
* pTotal
, LONGLONG
* pAvailable
)
1157 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1159 TRACE("(%p, %p)\n", pTotal
, pAvailable
);
1161 if (((dwSizeLow
= GetFileSize(This
->hFile
, &dwSizeHigh
)) == -1) &&
1162 (GetLastError() != NO_ERROR
))
1163 return HRESULT_FROM_WIN32(GetLastError());
1165 *pTotal
= (LONGLONG
)dwSizeLow
| (LONGLONG
)dwSizeHigh
<< (sizeof(DWORD
) * 8);
1167 *pAvailable
= *pTotal
;
1172 static HRESULT WINAPI
FileAsyncReader_BeginFlush(IAsyncReader
* iface
)
1174 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1178 This
->bFlushing
= TRUE
;
1179 CancelIo(This
->hFile
);
1180 SetEvent(This
->hEvent
);
1182 /* FIXME: free list */
1187 static HRESULT WINAPI
FileAsyncReader_EndFlush(IAsyncReader
* iface
)
1189 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1193 This
->bFlushing
= FALSE
;
1198 static const IAsyncReaderVtbl FileAsyncReader_Vtbl
=
1200 FileAsyncReader_QueryInterface
,
1201 FileAsyncReader_AddRef
,
1202 FileAsyncReader_Release
,
1203 FileAsyncReader_RequestAllocator
,
1204 FileAsyncReader_Request
,
1205 FileAsyncReader_WaitForNext
,
1206 FileAsyncReader_SyncReadAligned
,
1207 FileAsyncReader_SyncRead
,
1208 FileAsyncReader_Length
,
1209 FileAsyncReader_BeginFlush
,
1210 FileAsyncReader_EndFlush
,