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 #include "quartz_private.h"
23 #include "wine/debug.h"
24 #include "wine/unicode.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(quartz
);
35 static const WCHAR wszOutputPinName
[] = { 'O','u','t','p','u','t',0 };
37 typedef struct AsyncReader
39 const struct IBaseFilterVtbl
* lpVtbl
;
40 const struct IFileSourceFilterVtbl
* lpVtblFSF
;
43 FILTER_INFO filterInfo
;
45 CRITICAL_SECTION csFilter
;
52 static const struct IBaseFilterVtbl AsyncReader_Vtbl
;
53 static const struct IFileSourceFilterVtbl FileSource_Vtbl
;
54 static const struct IAsyncReaderVtbl FileAsyncReader_Vtbl
;
56 static HRESULT
FileAsyncReader_Construct(HANDLE hFile
, IBaseFilter
* pBaseFilter
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
);
58 #define _IFileSourceFilter_Offset ((int)(&(((AsyncReader*)0)->lpVtblFSF)))
59 #define ICOM_THIS_From_IFileSourceFilter(impl, iface) impl* This = (impl*)(((char*)iface)-_IFileSourceFilter_Offset);
61 #define _IAsyncReader_Offset ((int)(&(((FileAsyncReader*)0)->lpVtblAR)))
62 #define ICOM_THIS_From_IAsyncReader(impl, iface) impl* This = (impl*)(((char*)iface)-_IAsyncReader_Offset);
64 static HRESULT
process_extensions(HKEY hkeyExtensions
, LPCOLESTR pszFileName
, GUID
* majorType
, GUID
* minorType
)
66 /* FIXME: implement */
70 static unsigned char byte_from_hex_char(WCHAR wHex
)
72 switch (tolowerW(wHex
))
91 return wHex
- 'a' + 10;
97 static HRESULT
process_pattern_string(LPCWSTR wszPatternString
, IAsyncReader
* pReader
)
107 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString
));
109 /* format: "offset, bytestocompare, mask, value" */
111 ulOffset
= strtolW(wszPatternString
, NULL
, 10);
113 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
116 wszPatternString
++; /* skip ',' */
118 ulBytes
= strtolW(wszPatternString
, NULL
, 10);
120 pbMask
= HeapAlloc(GetProcessHeap(), 0, ulBytes
);
121 pbValue
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, ulBytes
);
122 pbFile
= HeapAlloc(GetProcessHeap(), 0, ulBytes
);
124 /* default mask is match everything */
125 memset(pbMask
, 0xFF, ulBytes
);
127 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
130 wszPatternString
++; /* skip ',' */
134 for ( ; !isxdigitW(*wszPatternString
) && (*wszPatternString
!= ','); wszPatternString
++)
137 for (strpos
= 0; isxdigitW(*wszPatternString
) && (strpos
/2 < ulBytes
); wszPatternString
++, strpos
++)
139 if ((strpos
% 2) == 1) /* odd numbered position */
140 pbMask
[strpos
/ 2] |= byte_from_hex_char(*wszPatternString
);
142 pbMask
[strpos
/ 2] = byte_from_hex_char(*wszPatternString
) << 4;
145 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
148 wszPatternString
++; /* skip ',' */
153 for ( ; !isxdigitW(*wszPatternString
) && (*wszPatternString
!= ','); wszPatternString
++)
156 for (strpos
= 0; isxdigitW(*wszPatternString
) && (strpos
/2 < ulBytes
); wszPatternString
++, strpos
++)
158 if ((strpos
% 2) == 1) /* odd numbered position */
159 pbValue
[strpos
/ 2] |= byte_from_hex_char(*wszPatternString
);
161 pbValue
[strpos
/ 2] = byte_from_hex_char(*wszPatternString
) << 4;
166 hr
= IAsyncReader_SyncRead(pReader
, ulOffset
, ulBytes
, pbFile
);
171 for (i
= 0; i
< ulBytes
; i
++)
172 if ((pbFile
[i
] & pbMask
[i
]) != pbValue
[i
])
179 HeapFree(GetProcessHeap(), 0, pbMask
);
180 HeapFree(GetProcessHeap(), 0, pbValue
);
181 HeapFree(GetProcessHeap(), 0, pbFile
);
183 /* if we encountered no errors with this string, and there is a following tuple, then we
184 * have to match that as well to succeed */
185 if ((hr
== S_OK
) && (wszPatternString
= strchrW(wszPatternString
, ',')))
186 return process_pattern_string(wszPatternString
+ 1, pReader
);
191 static HRESULT
GetClassMediaFile(IAsyncReader
* pReader
, LPCOLESTR pszFileName
, GUID
* majorType
, GUID
* minorType
)
193 HKEY hkeyMediaType
= NULL
;
196 WCHAR wszMediaType
[] = {'M','e','d','i','a',' ','T','y','p','e',0};
198 CopyMemory(majorType
, &GUID_NULL
, sizeof(*majorType
));
199 CopyMemory(minorType
, &GUID_NULL
, sizeof(*minorType
));
201 hr
= HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_CLASSES_ROOT
, wszMediaType
, 0, KEY_READ
, &hkeyMediaType
));
207 for (indexMajor
= 0; !bFound
; indexMajor
++)
210 WCHAR wszMajorKeyName
[CHARS_IN_GUID
];
211 DWORD dwKeyNameLength
= sizeof(wszMajorKeyName
) / sizeof(wszMajorKeyName
[0]);
212 const WCHAR wszExtensions
[] = {'E','x','t','e','n','s','i','o','n','s',0};
214 if (RegEnumKeyExW(hkeyMediaType
, indexMajor
, wszMajorKeyName
, &dwKeyNameLength
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
216 if (RegOpenKeyExW(hkeyMediaType
, wszMajorKeyName
, 0, KEY_READ
, &hkeyMajor
) != ERROR_SUCCESS
)
218 TRACE("%s\n", debugstr_w(wszMajorKeyName
));
219 if (!strcmpW(wszExtensions
, wszMajorKeyName
))
221 if (process_extensions(hkeyMajor
, pszFileName
, majorType
, minorType
) == S_OK
)
228 for (indexMinor
= 0; !bFound
; indexMinor
++)
231 WCHAR wszMinorKeyName
[CHARS_IN_GUID
];
232 DWORD dwMinorKeyNameLen
= sizeof(wszMinorKeyName
) / sizeof(wszMinorKeyName
[0]);
236 if (RegEnumKeyExW(hkeyMajor
, indexMinor
, wszMinorKeyName
, &dwMinorKeyNameLen
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
239 if (RegOpenKeyExW(hkeyMajor
, wszMinorKeyName
, 0, KEY_READ
, &hkeyMinor
) != ERROR_SUCCESS
)
242 TRACE("\t%s\n", debugstr_w(wszMinorKeyName
));
244 if (RegQueryInfoKeyW(hkeyMinor
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &maxValueLen
, NULL
, NULL
) != ERROR_SUCCESS
)
247 for (indexValue
= 0; !bFound
; indexValue
++)
250 WCHAR wszValueName
[14]; /* longest name we should encounter will be "Source Filter" */
251 LPWSTR wszPatternString
= HeapAlloc(GetProcessHeap(), 0, maxValueLen
);
252 DWORD dwValueNameLen
= sizeof(wszValueName
) / sizeof(wszValueName
[0]); /* remember this is in chars */
253 DWORD dwDataLen
= maxValueLen
; /* remember this is in bytes */
254 const WCHAR wszSourceFilter
[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
257 if ((temp
= RegEnumValueW(hkeyMinor
, indexValue
, wszValueName
, &dwValueNameLen
, NULL
, &dwType
, (LPBYTE
)wszPatternString
, &dwDataLen
)) != ERROR_SUCCESS
)
259 HeapFree(GetProcessHeap(), 0, wszPatternString
);
263 /* if it is not the source filter value */
264 if (strcmpW(wszValueName
, wszSourceFilter
))
266 if (process_pattern_string(wszPatternString
, pReader
) == S_OK
)
268 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName
, majorType
)) &&
269 SUCCEEDED(CLSIDFromString(wszMinorKeyName
, minorType
)))
273 HeapFree(GetProcessHeap(), 0, wszPatternString
);
275 CloseHandle(hkeyMinor
);
278 CloseHandle(hkeyMajor
);
281 CloseHandle(hkeyMediaType
);
283 if (SUCCEEDED(hr
) && !bFound
)
285 ERR("Media class not found\n");
289 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType
), qzdebugstr_guid(minorType
));
294 HRESULT
AsyncReader_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
296 AsyncReader
* pAsyncRead
= CoTaskMemAlloc(sizeof(AsyncReader
));
299 return E_OUTOFMEMORY
;
301 pAsyncRead
->lpVtbl
= &AsyncReader_Vtbl
;
302 pAsyncRead
->lpVtblFSF
= &FileSource_Vtbl
;
303 pAsyncRead
->refCount
= 1;
304 pAsyncRead
->filterInfo
.achName
[0] = '\0';
305 pAsyncRead
->filterInfo
.pGraph
= NULL
;
306 pAsyncRead
->pOutputPin
= NULL
;
308 InitializeCriticalSection(&pAsyncRead
->csFilter
);
310 pAsyncRead
->pszFileName
= NULL
;
311 pAsyncRead
->pmt
= NULL
;
313 *ppv
= (LPVOID
)pAsyncRead
;
315 TRACE("-- created at %p\n", pAsyncRead
);
320 /** IUnkown methods **/
322 static HRESULT WINAPI
AsyncReader_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
324 ICOM_THIS(AsyncReader
, iface
);
326 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
330 if (IsEqualIID(riid
, &IID_IUnknown
))
332 else if (IsEqualIID(riid
, &IID_IPersist
))
334 else if (IsEqualIID(riid
, &IID_IMediaFilter
))
336 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
338 else if (IsEqualIID(riid
, &IID_IFileSourceFilter
))
339 *ppv
= (LPVOID
)(&This
->lpVtblFSF
);
343 IUnknown_AddRef((IUnknown
*)(*ppv
));
347 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
349 return E_NOINTERFACE
;
352 static ULONG WINAPI
AsyncReader_AddRef(IBaseFilter
* iface
)
354 ICOM_THIS(AsyncReader
, iface
);
358 return InterlockedIncrement(&This
->refCount
);
361 static ULONG WINAPI
AsyncReader_Release(IBaseFilter
* iface
)
363 ICOM_THIS(AsyncReader
, iface
);
367 if (!InterlockedDecrement(&This
->refCount
))
369 if (This
->pOutputPin
)
370 IPin_Release(This
->pOutputPin
);
371 DeleteCriticalSection(&This
->csFilter
);
377 return This
->refCount
;
380 /** IPersist methods **/
382 static HRESULT WINAPI
AsyncReader_GetClassID(IBaseFilter
* iface
, CLSID
* pClsid
)
384 TRACE("(%p)\n", pClsid
);
386 *pClsid
= CLSID_AsyncReader
;
391 /** IMediaFilter methods **/
393 static HRESULT WINAPI
AsyncReader_Stop(IBaseFilter
* iface
)
395 ICOM_THIS(AsyncReader
, iface
);
399 This
->state
= State_Stopped
;
404 static HRESULT WINAPI
AsyncReader_Pause(IBaseFilter
* iface
)
406 ICOM_THIS(AsyncReader
, iface
);
410 This
->state
= State_Paused
;
415 static HRESULT WINAPI
AsyncReader_Run(IBaseFilter
* iface
, REFERENCE_TIME tStart
)
417 ICOM_THIS(AsyncReader
, iface
);
419 TRACE("(%lx%08lx)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
);
421 This
->state
= State_Running
;
426 static HRESULT WINAPI
AsyncReader_GetState(IBaseFilter
* iface
, DWORD dwMilliSecsTimeout
, FILTER_STATE
*pState
)
428 ICOM_THIS(AsyncReader
, iface
);
430 TRACE("(%lu, %p)\n", dwMilliSecsTimeout
, pState
);
432 *pState
= This
->state
;
437 static HRESULT WINAPI
AsyncReader_SetSyncSource(IBaseFilter
* iface
, IReferenceClock
*pClock
)
439 /* ICOM_THIS(AsyncReader, iface);*/
441 TRACE("(%p)\n", pClock
);
446 static HRESULT WINAPI
AsyncReader_GetSyncSource(IBaseFilter
* iface
, IReferenceClock
**ppClock
)
448 /* ICOM_THIS(AsyncReader, iface);*/
450 TRACE("(%p)\n", ppClock
);
455 /** IBaseFilter methods **/
457 static HRESULT WINAPI
AsyncReader_EnumPins(IBaseFilter
* iface
, IEnumPins
**ppEnum
)
460 ICOM_THIS(AsyncReader
, iface
);
462 TRACE("(%p)\n", ppEnum
);
464 epd
.cPins
= This
->pOutputPin
? 1 : 0;
465 epd
.ppPins
= &This
->pOutputPin
;
466 return IEnumPinsImpl_Construct(&epd
, ppEnum
);
469 static HRESULT WINAPI
AsyncReader_FindPin(IBaseFilter
* iface
, LPCWSTR Id
, IPin
**ppPin
)
471 FIXME("(%s, %p)\n", debugstr_w(Id
), ppPin
);
476 static HRESULT WINAPI
AsyncReader_QueryFilterInfo(IBaseFilter
* iface
, FILTER_INFO
*pInfo
)
478 ICOM_THIS(AsyncReader
, iface
);
480 TRACE("(%p)\n", pInfo
);
482 strcpyW(pInfo
->achName
, This
->filterInfo
.achName
);
483 pInfo
->pGraph
= This
->filterInfo
.pGraph
;
486 IFilterGraph_AddRef(pInfo
->pGraph
);
491 static HRESULT WINAPI
AsyncReader_JoinFilterGraph(IBaseFilter
* iface
, IFilterGraph
*pGraph
, LPCWSTR pName
)
493 ICOM_THIS(AsyncReader
, iface
);
495 TRACE("(%p, %s)\n", pGraph
, debugstr_w(pName
));
498 strcpyW(This
->filterInfo
.achName
, pName
);
500 *This
->filterInfo
.achName
= 0;
501 This
->filterInfo
.pGraph
= pGraph
; /* NOTE: do NOT increase ref. count */
506 static HRESULT WINAPI
AsyncReader_QueryVendorInfo(IBaseFilter
* iface
, LPWSTR
*pVendorInfo
)
508 FIXME("(%p)\n", pVendorInfo
);
513 static const IBaseFilterVtbl AsyncReader_Vtbl
=
515 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
516 AsyncReader_QueryInterface
,
519 AsyncReader_GetClassID
,
523 AsyncReader_GetState
,
524 AsyncReader_SetSyncSource
,
525 AsyncReader_GetSyncSource
,
526 AsyncReader_EnumPins
,
528 AsyncReader_QueryFilterInfo
,
529 AsyncReader_JoinFilterGraph
,
530 AsyncReader_QueryVendorInfo
533 static HRESULT WINAPI
FileSource_QueryInterface(IFileSourceFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
535 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
537 return IBaseFilter_QueryInterface((IFileSourceFilter
*)&This
->lpVtbl
, riid
, ppv
);
540 static ULONG WINAPI
FileSource_AddRef(IFileSourceFilter
* iface
)
542 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
544 return IBaseFilter_AddRef((IFileSourceFilter
*)&This
->lpVtbl
);
547 static ULONG WINAPI
FileSource_Release(IFileSourceFilter
* iface
)
549 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
551 return IBaseFilter_Release((IFileSourceFilter
*)&This
->lpVtbl
);
554 static HRESULT WINAPI
FileSource_Load(IFileSourceFilter
* iface
, LPCOLESTR pszFileName
, const AM_MEDIA_TYPE
* pmt
)
558 IAsyncReader
* pReader
= NULL
;
559 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
561 TRACE("(%s, %p)\n", debugstr_w(pszFileName
), pmt
);
564 /* FIXME: check the sharing values that native uses */
565 hFile
= CreateFileW(pszFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, NULL
);
567 if (hFile
== INVALID_HANDLE_VALUE
)
569 return HRESULT_FROM_WIN32(GetLastError());
573 hr
= FileAsyncReader_Construct(hFile
, (IBaseFilter
*)&This
->lpVtbl
, &This
->csFilter
, &This
->pOutputPin
);
576 hr
= IPin_QueryInterface(This
->pOutputPin
, &IID_IAsyncReader
, (LPVOID
*)&pReader
);
578 /* store file name & media type */
581 This
->pszFileName
= CoTaskMemAlloc((strlenW(pszFileName
) + 1) * sizeof(WCHAR
));
582 strcpyW(This
->pszFileName
, pszFileName
);
583 This
->pmt
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
));
586 This
->pmt
->bFixedSizeSamples
= TRUE
;
587 This
->pmt
->bTemporalCompression
= FALSE
;
588 This
->pmt
->cbFormat
= 0;
589 This
->pmt
->pbFormat
= NULL
;
590 This
->pmt
->pUnk
= NULL
;
591 This
->pmt
->lSampleSize
= 0;
592 memcpy(&This
->pmt
->formattype
, &FORMAT_None
, sizeof(FORMAT_None
));
593 hr
= GetClassMediaFile(pReader
, pszFileName
, &This
->pmt
->majortype
, &This
->pmt
->subtype
);
596 CoTaskMemFree(This
->pmt
);
601 CopyMediaType(This
->pmt
, pmt
);
605 IAsyncReader_Release(pReader
);
609 if (This
->pOutputPin
)
611 IPin_Release(This
->pOutputPin
);
612 This
->pOutputPin
= NULL
;
614 if (This
->pszFileName
)
616 CoTaskMemFree(This
->pszFileName
);
617 This
->pszFileName
= NULL
;
622 /* FIXME: check return codes */
626 static HRESULT WINAPI
FileSource_GetCurFile(IFileSourceFilter
* iface
, LPOLESTR
* ppszFileName
, AM_MEDIA_TYPE
* pmt
)
628 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
630 TRACE("(%p, %p)\n", ppszFileName
, pmt
);
632 /* copy file name & media type if available, otherwise clear the outputs */
633 if (This
->pszFileName
)
635 *ppszFileName
= CoTaskMemAlloc((strlenW(This
->pszFileName
) + 1) * sizeof(WCHAR
));
636 strcpyW(*ppszFileName
, This
->pszFileName
);
639 *ppszFileName
= NULL
;
643 CopyMediaType(pmt
, This
->pmt
);
646 ZeroMemory(pmt
, sizeof(*pmt
));
651 static const IFileSourceFilterVtbl FileSource_Vtbl
=
653 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
654 FileSource_QueryInterface
,
658 FileSource_GetCurFile
662 /* the dwUserData passed back to user */
663 typedef struct DATAREQUEST
665 IMediaSample
* pSample
; /* sample passed to us by user */
666 DWORD_PTR dwUserData
; /* user data passed to us */
667 OVERLAPPED ovl
; /* our overlapped structure */
669 struct DATAREQUEST
* pNext
; /* next data request in list */
672 void queue(DATAREQUEST
* pHead
, DATAREQUEST
* pItem
)
674 DATAREQUEST
* pCurrent
;
675 for (pCurrent
= pHead
; pCurrent
->pNext
; pCurrent
= pCurrent
->pNext
)
677 pCurrent
->pNext
= pItem
;
680 typedef struct FileAsyncReader
683 const struct IAsyncReaderVtbl
* lpVtblAR
;
688 DATAREQUEST
* pHead
; /* head of data request list */
689 CRITICAL_SECTION csList
; /* critical section to protect operations on list */
692 static HRESULT
AcceptProcAFR(LPVOID iface
, const AM_MEDIA_TYPE
*pmt
)
694 ICOM_THIS(AsyncReader
, iface
);
696 FIXME("(%p, %p)\n", iface
, pmt
);
698 if (IsEqualGUID(&pmt
->majortype
, &This
->pmt
->majortype
) &&
699 IsEqualGUID(&pmt
->subtype
, &This
->pmt
->subtype
) &&
700 IsEqualGUID(&pmt
->formattype
, &FORMAT_None
))
706 /* overriden pin functions */
708 static HRESULT WINAPI
FileAsyncReaderPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
710 ICOM_THIS(FileAsyncReader
, iface
);
711 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
715 if (IsEqualIID(riid
, &IID_IUnknown
))
717 else if (IsEqualIID(riid
, &IID_IPin
))
719 else if (IsEqualIID(riid
, &IID_IAsyncReader
))
720 *ppv
= (LPVOID
)&This
->lpVtblAR
;
724 IUnknown_AddRef((IUnknown
*)(*ppv
));
728 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
730 return E_NOINTERFACE
;
733 static ULONG WINAPI
FileAsyncReaderPin_Release(IPin
* iface
)
735 ICOM_THIS(FileAsyncReader
, iface
);
739 if (!InterlockedDecrement(&This
->pin
.pin
.refCount
))
741 DATAREQUEST
* pCurrent
;
743 for (pCurrent
= This
->pHead
; pCurrent
; pCurrent
= pNext
)
745 pNext
= pCurrent
->pNext
;
746 CoTaskMemFree(pCurrent
);
748 CloseHandle(This
->hFile
);
749 CloseHandle(This
->hEvent
);
753 return This
->pin
.pin
.refCount
;
756 static HRESULT WINAPI
FileAsyncReaderPin_EnumMediaTypes(IPin
* iface
, IEnumMediaTypes
** ppEnum
)
758 ENUMMEDIADETAILS emd
;
759 ICOM_THIS(FileAsyncReader
, iface
);
761 TRACE("(%p)\n", ppEnum
);
764 emd
.pMediaTypes
= ((AsyncReader
*)This
->pin
.pin
.pinInfo
.pFilter
)->pmt
;
766 return IEnumMediaTypesImpl_Construct(&emd
, ppEnum
);
769 static HRESULT WINAPI
FileAsyncReaderPin_Connect(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
772 ICOM_THIS(OutputPin
, iface
);
774 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
775 dump_AM_MEDIA_TYPE(pmt
);
777 /* If we try to connect to ourself, we will definitely deadlock.
778 * There are other cases where we could deadlock too, but this
779 * catches the obvious case */
780 assert(pReceivePin
!= iface
);
782 EnterCriticalSection(This
->pin
.pCritSec
);
784 /* if we have been a specific type to connect with, then we can either connect
785 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
786 if (pmt
&& !IsEqualGUID(&pmt
->majortype
, &GUID_NULL
) && !IsEqualGUID(&pmt
->subtype
, &GUID_NULL
))
787 hr
= This
->pConnectSpecific(iface
, pReceivePin
, pmt
);
790 /* negotiate media type */
792 IEnumMediaTypes
* pEnumCandidates
;
793 AM_MEDIA_TYPE
* pmtCandidate
; /* Candidate media type */
795 if (SUCCEEDED(IPin_EnumMediaTypes(iface
, &pEnumCandidates
)))
797 hr
= VFW_E_NO_ACCEPTABLE_TYPES
; /* Assume the worst, but set to S_OK if connected successfully */
799 /* try this filter's media types first */
800 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
802 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
803 (This
->pConnectSpecific(iface
, pReceivePin
, pmtCandidate
) == S_OK
))
806 CoTaskMemFree(pmtCandidate
);
809 CoTaskMemFree(pmtCandidate
);
811 IEnumMediaTypes_Release(pEnumCandidates
);
814 /* then try receiver filter's media types */
815 if (hr
!= S_OK
&& SUCCEEDED(IPin_EnumMediaTypes(pReceivePin
, &pEnumCandidates
))) /* if we haven't already connected successfully */
817 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
819 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
820 (This
->pConnectSpecific(iface
, pReceivePin
, pmtCandidate
) == S_OK
))
823 CoTaskMemFree(pmtCandidate
);
826 CoTaskMemFree(pmtCandidate
);
828 IEnumMediaTypes_Release(pEnumCandidates
);
830 } /* if negotiate media type */
832 LeaveCriticalSection(This
->pin
.pCritSec
);
834 TRACE("-- %lx\n", hr
);
838 static const IPinVtbl FileAsyncReaderPin_Vtbl
=
840 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
841 FileAsyncReaderPin_QueryInterface
,
843 FileAsyncReaderPin_Release
,
844 FileAsyncReaderPin_Connect
,
845 OutputPin_ReceiveConnection
,
847 IPinImpl_ConnectedTo
,
848 IPinImpl_ConnectionMediaType
,
849 IPinImpl_QueryPinInfo
,
850 IPinImpl_QueryDirection
,
852 IPinImpl_QueryAccept
,
853 FileAsyncReaderPin_EnumMediaTypes
,
854 IPinImpl_QueryInternalConnections
,
855 OutputPin_EndOfStream
,
856 OutputPin_BeginFlush
,
861 static HRESULT
FileAsyncReader_Construct(HANDLE hFile
, IBaseFilter
* pBaseFilter
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
863 FileAsyncReader
* pPinImpl
;
868 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
871 return E_OUTOFMEMORY
;
873 piOutput
.dir
= PINDIR_OUTPUT
;
874 piOutput
.pFilter
= pBaseFilter
;
875 strcpyW(piOutput
.achName
, wszOutputPinName
);
877 if (SUCCEEDED(OutputPin_Init(&piOutput
, NULL
, pBaseFilter
, AcceptProcAFR
, pCritSec
, &pPinImpl
->pin
)))
879 pPinImpl
->pin
.pin
.lpVtbl
= &FileAsyncReaderPin_Vtbl
;
880 pPinImpl
->lpVtblAR
= &FileAsyncReader_Vtbl
;
881 pPinImpl
->hFile
= hFile
;
882 pPinImpl
->hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
883 pPinImpl
->bFlushing
= FALSE
;
884 pPinImpl
->pHead
= NULL
;
886 *ppPin
= (IPin
*)(&pPinImpl
->pin
.pin
.lpVtbl
);
894 static HRESULT WINAPI
FileAsyncReader_QueryInterface(IAsyncReader
* iface
, REFIID riid
, LPVOID
* ppv
)
896 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
898 return IPin_QueryInterface((IPin
*)This
, riid
, ppv
);
901 static ULONG WINAPI
FileAsyncReader_AddRef(IAsyncReader
* iface
)
903 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
905 return IPin_AddRef((IPin
*)This
);
908 static ULONG WINAPI
FileAsyncReader_Release(IAsyncReader
* iface
)
910 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
912 return IPin_Release((IPin
*)This
);
915 #define DEF_ALIGNMENT 1
917 static HRESULT WINAPI
FileAsyncReader_RequestAllocator(IAsyncReader
* iface
, IMemAllocator
* pPreferred
, ALLOCATOR_PROPERTIES
* pProps
, IMemAllocator
** ppActual
)
921 TRACE("(%p, %p, %p)\n", pPreferred
, pProps
, ppActual
);
923 if (!pProps
->cbAlign
|| (pProps
->cbAlign
% DEF_ALIGNMENT
) != 0)
924 pProps
->cbAlign
= DEF_ALIGNMENT
;
928 ALLOCATOR_PROPERTIES PropsActual
;
929 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, &PropsActual
);
930 /* FIXME: check we are still aligned */
933 IMemAllocator_AddRef(pPreferred
);
934 *ppActual
= pPreferred
;
935 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr
);
942 hr
= CoCreateInstance(&CLSID_MemoryAllocator
, NULL
, CLSCTX_INPROC
, &IID_IMemAllocator
, (LPVOID
*)&pPreferred
);
946 ALLOCATOR_PROPERTIES PropsActual
;
947 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, &PropsActual
);
948 /* FIXME: check we are still aligned */
951 IMemAllocator_AddRef(pPreferred
);
952 *ppActual
= pPreferred
;
953 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr
);
962 IMemAllocator_Release(pPreferred
);
965 TRACE("-- %lx\n", hr
);
969 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
970 * however, this would be quite complicated to do and may be a bit error prone */
971 static HRESULT WINAPI
FileAsyncReader_Request(IAsyncReader
* iface
, IMediaSample
* pSample
, DWORD_PTR dwUser
)
973 REFERENCE_TIME Start
;
975 DATAREQUEST
* pDataRq
;
978 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
980 TRACE("(%p, %lx)\n", pSample
, dwUser
);
982 /* check flushing state */
984 return VFW_E_WRONG_STATE
;
986 if (!(pDataRq
= CoTaskMemAlloc(sizeof(*pDataRq
))))
989 /* get start and stop positions in bytes */
991 hr
= IMediaSample_GetTime(pSample
, &Start
, &Stop
);
994 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
998 DWORD dwLength
= (DWORD
) BYTES_FROM_MEDIATIME(Stop
- Start
);
1000 pDataRq
->ovl
.Offset
= (DWORD
) BYTES_FROM_MEDIATIME(Start
);
1001 pDataRq
->ovl
.OffsetHigh
= (DWORD
)(BYTES_FROM_MEDIATIME(Start
) >> (sizeof(DWORD
) * 8));
1002 pDataRq
->ovl
.hEvent
= This
->hEvent
;
1003 pDataRq
->dwUserData
= dwUser
;
1004 pDataRq
->pNext
= NULL
;
1005 /* we violate traditional COM rules here by maintaining
1006 * a reference to the sample, but not calling AddRef, but
1007 * that's what MSDN says to do */
1008 pDataRq
->pSample
= pSample
;
1010 EnterCriticalSection(&This
->csList
);
1013 /* adds data request to end of list */
1014 queue(This
->pHead
, pDataRq
);
1016 This
->pHead
= pDataRq
;
1018 LeaveCriticalSection(&This
->csList
);
1020 /* this is definitely not how it is implemented on Win9x
1021 * as they do not support async reads on files, but it is
1022 * sooo much easier to use this than messing around with threads!
1024 if (!ReadFile(This
->hFile
, pBuffer
, dwLength
, NULL
, &pDataRq
->ovl
))
1025 hr
= HRESULT_FROM_WIN32(GetLastError());
1027 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1028 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1032 if (FAILED(hr
) && pDataRq
)
1034 EnterCriticalSection(&This
->csList
);
1036 DATAREQUEST
* pCurrent
;
1037 for (pCurrent
= This
->pHead
; pCurrent
&& pCurrent
->pNext
; pCurrent
= pCurrent
->pNext
)
1038 if (pCurrent
->pNext
== pDataRq
)
1040 pCurrent
->pNext
= pDataRq
->pNext
;
1044 LeaveCriticalSection(&This
->csList
);
1045 CoTaskMemFree(pDataRq
);
1048 TRACE("-- %lx\n", hr
);
1052 static HRESULT WINAPI
FileAsyncReader_WaitForNext(IAsyncReader
* iface
, DWORD dwTimeout
, IMediaSample
** ppSample
, DWORD_PTR
* pdwUser
)
1055 DATAREQUEST
* pDataRq
= NULL
;
1056 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1058 TRACE("(%lu, %p, %p)\n", dwTimeout
, ppSample
, pdwUser
);
1060 /* FIXME: we could do with improving this by waiting for an array of event handles
1061 * and then determining which one finished and removing that from the list, otherwise
1062 * we will end up waiting for longer than we should do, if a later request finishes
1063 * before an earlier one */
1068 /* we return immediately if flushing */
1069 if (This
->bFlushing
)
1070 hr
= VFW_E_WRONG_STATE
;
1074 /* wait for the read to finish or timeout */
1075 if (WaitForSingleObject(This
->hEvent
, dwTimeout
) == WAIT_TIMEOUT
)
1080 EnterCriticalSection(&This
->csList
);
1082 pDataRq
= This
->pHead
;
1083 if (pDataRq
== NULL
)
1086 This
->pHead
= pDataRq
->pNext
;
1088 LeaveCriticalSection(&This
->csList
);
1094 /* get any errors */
1095 if (!GetOverlappedResult(This
->hFile
, &pDataRq
->ovl
, &dwBytes
, TRUE
))
1096 hr
= HRESULT_FROM_WIN32(GetLastError());
1101 *ppSample
= pDataRq
->pSample
;
1102 *pdwUser
= pDataRq
->dwUserData
;
1108 /* no need to close event handle since we will close it when the pin is destroyed */
1109 CoTaskMemFree(pDataRq
);
1112 TRACE("-- %lx\n", hr
);
1116 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
);
1118 static HRESULT WINAPI
FileAsyncReader_SyncReadAligned(IAsyncReader
* iface
, IMediaSample
* pSample
)
1121 REFERENCE_TIME tStart
;
1122 REFERENCE_TIME tStop
;
1125 TRACE("(%p)\n", pSample
);
1127 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
1130 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
1133 hr
= FileAsyncReader_SyncRead(iface
,
1134 BYTES_FROM_MEDIATIME(tStart
),
1135 (LONG
) BYTES_FROM_MEDIATIME(tStop
- tStart
),
1138 TRACE("-- %lx\n", hr
);
1142 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
)
1146 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1148 TRACE("(%lx%08lx, %ld, %p)\n", (ULONG
)(llPosition
>> 32), (ULONG
)llPosition
, lLength
, pBuffer
);
1150 ZeroMemory(&ovl
, sizeof(ovl
));
1152 ovl
.hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
1153 /* NOTE: llPosition is the actual byte position to start reading from */
1154 ovl
.Offset
= (DWORD
) llPosition
;
1155 ovl
.OffsetHigh
= (DWORD
) (llPosition
>> (sizeof(DWORD
) * 8));
1157 if (!ReadFile(This
->hFile
, pBuffer
, lLength
, NULL
, &ovl
))
1158 hr
= HRESULT_FROM_WIN32(GetLastError());
1160 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1167 if (!GetOverlappedResult(This
->hFile
, &ovl
, &dwBytesRead
, TRUE
))
1168 hr
= HRESULT_FROM_WIN32(GetLastError());
1171 CloseHandle(ovl
.hEvent
);
1173 TRACE("-- %lx\n", hr
);
1177 static HRESULT WINAPI
FileAsyncReader_Length(IAsyncReader
* iface
, LONGLONG
* pTotal
, LONGLONG
* pAvailable
)
1181 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1183 TRACE("(%p, %p)\n", pTotal
, pAvailable
);
1185 if (((dwSizeLow
= GetFileSize(This
->hFile
, &dwSizeHigh
)) == -1) &&
1186 (GetLastError() != NO_ERROR
))
1187 return HRESULT_FROM_WIN32(GetLastError());
1189 *pTotal
= (LONGLONG
)dwSizeLow
| (LONGLONG
)dwSizeHigh
<< (sizeof(DWORD
) * 8);
1191 *pAvailable
= *pTotal
;
1196 static HRESULT WINAPI
FileAsyncReader_BeginFlush(IAsyncReader
* iface
)
1198 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1202 This
->bFlushing
= TRUE
;
1203 CancelIo(This
->hFile
);
1204 SetEvent(This
->hEvent
);
1206 /* FIXME: free list */
1211 static HRESULT WINAPI
FileAsyncReader_EndFlush(IAsyncReader
* iface
)
1213 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1217 This
->bFlushing
= FALSE
;
1222 static const IAsyncReaderVtbl FileAsyncReader_Vtbl
=
1224 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1225 FileAsyncReader_QueryInterface
,
1226 FileAsyncReader_AddRef
,
1227 FileAsyncReader_Release
,
1228 FileAsyncReader_RequestAllocator
,
1229 FileAsyncReader_Request
,
1230 FileAsyncReader_WaitForNext
,
1231 FileAsyncReader_SyncReadAligned
,
1232 FileAsyncReader_SyncRead
,
1233 FileAsyncReader_Length
,
1234 FileAsyncReader_BeginFlush
,
1235 FileAsyncReader_EndFlush
,