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"
32 WINE_DEFAULT_DEBUG_CHANNEL(quartz
);
34 static const WCHAR wszOutputPinName
[] = { 'O','u','t','p','u','t',0 };
36 /* see IAsyncReader::Request on MSDN for the explanation of this */
37 #define BYTES_FROM_MEDIATIME(time) ((time) / 10000000)
39 typedef struct AsyncReader
41 const struct IBaseFilterVtbl
* lpVtbl
;
42 const struct IFileSourceFilterVtbl
* lpVtblFSF
;
45 FILTER_INFO filterInfo
;
47 CRITICAL_SECTION csFilter
;
54 static const struct IBaseFilterVtbl AsyncReader_Vtbl
;
55 static const struct IFileSourceFilterVtbl FileSource_Vtbl
;
56 static const struct IAsyncReaderVtbl FileAsyncReader_Vtbl
;
58 static HRESULT
FileAsyncReader_Construct(HANDLE hFile
, IBaseFilter
* pBaseFilter
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
);
60 #define _IFileSourceFilter_Offset ((int)(&(((AsyncReader*)0)->lpVtblFSF)))
61 #define ICOM_THIS_From_IFileSourceFilter(impl, iface) impl* This = (impl*)(((char*)iface)-_IFileSourceFilter_Offset);
63 #define _IAsyncReader_Offset ((int)(&(((FileAsyncReader*)0)->lpVtblAR)))
64 #define ICOM_THIS_From_IAsyncReader(impl, iface) impl* This = (impl*)(((char*)iface)-_IAsyncReader_Offset);
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 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 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 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
= CoTaskMemAlloc(sizeof(AsyncReader
));
301 return E_OUTOFMEMORY
;
303 pAsyncRead
->lpVtbl
= &AsyncReader_Vtbl
;
304 pAsyncRead
->lpVtblFSF
= &FileSource_Vtbl
;
305 pAsyncRead
->refCount
= 1;
306 pAsyncRead
->filterInfo
.achName
[0] = '\0';
307 pAsyncRead
->filterInfo
.pGraph
= NULL
;
308 pAsyncRead
->pOutputPin
= NULL
;
310 InitializeCriticalSection(&pAsyncRead
->csFilter
);
312 pAsyncRead
->pszFileName
= NULL
;
313 pAsyncRead
->pmt
= NULL
;
315 *ppv
= (LPVOID
)pAsyncRead
;
317 TRACE("-- created at %p\n", pAsyncRead
);
322 /** IUnkown methods **/
324 static HRESULT WINAPI
AsyncReader_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
326 ICOM_THIS(AsyncReader
, iface
);
328 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
332 if (IsEqualIID(riid
, &IID_IUnknown
))
334 else if (IsEqualIID(riid
, &IID_IPersist
))
336 else if (IsEqualIID(riid
, &IID_IMediaFilter
))
338 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
340 else if (IsEqualIID(riid
, &IID_IFileSourceFilter
))
341 *ppv
= (LPVOID
)(&This
->lpVtblFSF
);
345 IUnknown_AddRef((IUnknown
*)(*ppv
));
349 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
351 return E_NOINTERFACE
;
354 static ULONG WINAPI
AsyncReader_AddRef(IBaseFilter
* iface
)
356 ICOM_THIS(AsyncReader
, iface
);
360 return InterlockedIncrement(&This
->refCount
);
363 static ULONG WINAPI
AsyncReader_Release(IBaseFilter
* iface
)
365 ICOM_THIS(AsyncReader
, iface
);
369 if (!InterlockedDecrement(&This
->refCount
))
371 if (This
->pOutputPin
)
372 IPin_Release(This
->pOutputPin
);
373 DeleteCriticalSection(&This
->csFilter
);
379 return This
->refCount
;
382 /** IPersist methods **/
384 static HRESULT WINAPI
AsyncReader_GetClassID(IBaseFilter
* iface
, CLSID
* pClsid
)
386 TRACE("(%p)\n", pClsid
);
388 *pClsid
= CLSID_AsyncReader
;
393 /** IMediaFilter methods **/
395 static HRESULT WINAPI
AsyncReader_Stop(IBaseFilter
* iface
)
397 ICOM_THIS(AsyncReader
, iface
);
401 This
->state
= State_Stopped
;
406 static HRESULT WINAPI
AsyncReader_Pause(IBaseFilter
* iface
)
408 ICOM_THIS(AsyncReader
, iface
);
412 This
->state
= State_Paused
;
417 static HRESULT WINAPI
AsyncReader_Run(IBaseFilter
* iface
, REFERENCE_TIME tStart
)
419 ICOM_THIS(AsyncReader
, iface
);
421 TRACE("(%lx%08lx)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
);
423 This
->state
= State_Running
;
428 static HRESULT WINAPI
AsyncReader_GetState(IBaseFilter
* iface
, DWORD dwMilliSecsTimeout
, FILTER_STATE
*pState
)
430 ICOM_THIS(AsyncReader
, iface
);
432 TRACE("(%lu, %p)\n", dwMilliSecsTimeout
, pState
);
434 *pState
= This
->state
;
439 static HRESULT WINAPI
AsyncReader_SetSyncSource(IBaseFilter
* iface
, IReferenceClock
*pClock
)
441 /* ICOM_THIS(AsyncReader, iface);*/
443 TRACE("(%p)\n", pClock
);
448 static HRESULT WINAPI
AsyncReader_GetSyncSource(IBaseFilter
* iface
, IReferenceClock
**ppClock
)
450 /* ICOM_THIS(AsyncReader, iface);*/
452 TRACE("(%p)\n", ppClock
);
457 /** IBaseFilter methods **/
459 static HRESULT WINAPI
AsyncReader_EnumPins(IBaseFilter
* iface
, IEnumPins
**ppEnum
)
462 ICOM_THIS(AsyncReader
, iface
);
464 TRACE("(%p)\n", ppEnum
);
466 epd
.cPins
= This
->pOutputPin
? 1 : 0;
467 epd
.ppPins
= &This
->pOutputPin
;
468 return IEnumPinsImpl_Construct(&epd
, ppEnum
);
471 static HRESULT WINAPI
AsyncReader_FindPin(IBaseFilter
* iface
, LPCWSTR Id
, IPin
**ppPin
)
473 FIXME("(%s, %p)\n", debugstr_w(Id
), ppPin
);
478 static HRESULT WINAPI
AsyncReader_QueryFilterInfo(IBaseFilter
* iface
, FILTER_INFO
*pInfo
)
480 ICOM_THIS(AsyncReader
, iface
);
482 TRACE("(%p)\n", pInfo
);
484 strcpyW(pInfo
->achName
, This
->filterInfo
.achName
);
485 pInfo
->pGraph
= This
->filterInfo
.pGraph
;
488 IFilterGraph_AddRef(pInfo
->pGraph
);
493 static HRESULT WINAPI
AsyncReader_JoinFilterGraph(IBaseFilter
* iface
, IFilterGraph
*pGraph
, LPCWSTR pName
)
495 ICOM_THIS(AsyncReader
, iface
);
497 TRACE("(%p, %s)\n", pGraph
, debugstr_w(pName
));
500 strcpyW(This
->filterInfo
.achName
, pName
);
502 *This
->filterInfo
.achName
= 0;
503 This
->filterInfo
.pGraph
= pGraph
; /* NOTE: do NOT increase ref. count */
508 static HRESULT WINAPI
AsyncReader_QueryVendorInfo(IBaseFilter
* iface
, LPWSTR
*pVendorInfo
)
510 FIXME("(%p)\n", pVendorInfo
);
515 static const IBaseFilterVtbl AsyncReader_Vtbl
=
517 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
518 AsyncReader_QueryInterface
,
521 AsyncReader_GetClassID
,
525 AsyncReader_GetState
,
526 AsyncReader_SetSyncSource
,
527 AsyncReader_GetSyncSource
,
528 AsyncReader_EnumPins
,
530 AsyncReader_QueryFilterInfo
,
531 AsyncReader_JoinFilterGraph
,
532 AsyncReader_QueryVendorInfo
535 static HRESULT WINAPI
FileSource_QueryInterface(IFileSourceFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
537 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
539 return IBaseFilter_QueryInterface((IFileSourceFilter
*)&This
->lpVtbl
, riid
, ppv
);
542 static ULONG WINAPI
FileSource_AddRef(IFileSourceFilter
* iface
)
544 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
546 return IBaseFilter_AddRef((IFileSourceFilter
*)&This
->lpVtbl
);
549 static ULONG WINAPI
FileSource_Release(IFileSourceFilter
* iface
)
551 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
553 return IBaseFilter_Release((IFileSourceFilter
*)&This
->lpVtbl
);
556 static HRESULT WINAPI
FileSource_Load(IFileSourceFilter
* iface
, LPCOLESTR pszFileName
, const AM_MEDIA_TYPE
* pmt
)
560 IAsyncReader
* pReader
= NULL
;
561 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
563 TRACE("(%s, %p)\n", debugstr_w(pszFileName
), pmt
);
566 /* FIXME: check the sharing values that native uses */
567 hFile
= CreateFileW(pszFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, NULL
);
569 if (hFile
== INVALID_HANDLE_VALUE
)
571 return HRESULT_FROM_WIN32(GetLastError());
575 hr
= FileAsyncReader_Construct(hFile
, (IBaseFilter
*)&This
->lpVtbl
, &This
->csFilter
, &This
->pOutputPin
);
578 hr
= IPin_QueryInterface(This
->pOutputPin
, &IID_IAsyncReader
, (LPVOID
*)&pReader
);
580 /* store file name & media type */
583 This
->pszFileName
= CoTaskMemAlloc((strlenW(pszFileName
) + 1) * sizeof(WCHAR
));
584 strcpyW(This
->pszFileName
, pszFileName
);
585 This
->pmt
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
));
588 This
->pmt
->bFixedSizeSamples
= TRUE
;
589 This
->pmt
->bTemporalCompression
= FALSE
;
590 This
->pmt
->cbFormat
= 0;
591 This
->pmt
->pbFormat
= NULL
;
592 This
->pmt
->pUnk
= NULL
;
593 This
->pmt
->lSampleSize
= 0;
594 memcpy(&This
->pmt
->formattype
, &FORMAT_None
, sizeof(FORMAT_None
));
595 hr
= GetClassMediaFile(pReader
, pszFileName
, &This
->pmt
->majortype
, &This
->pmt
->subtype
);
598 CoTaskMemFree(This
->pmt
);
603 CopyMediaType(This
->pmt
, pmt
);
607 IAsyncReader_Release(pReader
);
611 if (This
->pOutputPin
)
613 IPin_Release(This
->pOutputPin
);
614 This
->pOutputPin
= NULL
;
616 if (This
->pszFileName
)
618 CoTaskMemFree(This
->pszFileName
);
619 This
->pszFileName
= NULL
;
624 /* FIXME: check return codes */
628 static HRESULT WINAPI
FileSource_GetCurFile(IFileSourceFilter
* iface
, LPOLESTR
* ppszFileName
, AM_MEDIA_TYPE
* pmt
)
630 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
632 TRACE("(%p, %p)\n", ppszFileName
, pmt
);
634 /* copy file name & media type if available, otherwise clear the outputs */
635 if (This
->pszFileName
)
637 *ppszFileName
= CoTaskMemAlloc((strlenW(This
->pszFileName
) + 1) * sizeof(WCHAR
));
638 strcpyW(*ppszFileName
, This
->pszFileName
);
641 *ppszFileName
= NULL
;
645 CopyMediaType(pmt
, This
->pmt
);
648 ZeroMemory(pmt
, sizeof(*pmt
));
653 static const IFileSourceFilterVtbl FileSource_Vtbl
=
655 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
656 FileSource_QueryInterface
,
660 FileSource_GetCurFile
664 /* the dwUserData passed back to user */
665 typedef struct DATAREQUEST
667 IMediaSample
* pSample
; /* sample passed to us by user */
668 DWORD_PTR dwUserData
; /* user data passed to us */
669 OVERLAPPED ovl
; /* our overlapped structure */
671 struct DATAREQUEST
* pNext
; /* next data request in list */
674 void queue(DATAREQUEST
* pHead
, DATAREQUEST
* pItem
)
676 DATAREQUEST
* pCurrent
;
677 for (pCurrent
= pHead
; pCurrent
->pNext
; pCurrent
= pCurrent
->pNext
)
679 pCurrent
->pNext
= pItem
;
682 typedef struct FileAsyncReader
685 const struct IAsyncReaderVtbl
* lpVtblAR
;
690 DATAREQUEST
* pHead
; /* head of data request list */
691 CRITICAL_SECTION csList
; /* critical section to protect operations on list */
694 static HRESULT
AcceptProcAFR(LPVOID iface
, const AM_MEDIA_TYPE
*pmt
)
696 ICOM_THIS(AsyncReader
, iface
);
698 FIXME("(%p, %p)\n", iface
, pmt
);
700 if (IsEqualGUID(&pmt
->majortype
, &This
->pmt
->majortype
) &&
701 IsEqualGUID(&pmt
->subtype
, &This
->pmt
->subtype
) &&
702 IsEqualGUID(&pmt
->formattype
, &FORMAT_None
))
708 /* overriden pin functions */
710 static HRESULT WINAPI
FileAsyncReaderPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
712 ICOM_THIS(FileAsyncReader
, iface
);
713 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
717 if (IsEqualIID(riid
, &IID_IUnknown
))
719 else if (IsEqualIID(riid
, &IID_IPin
))
721 else if (IsEqualIID(riid
, &IID_IAsyncReader
))
722 *ppv
= (LPVOID
)&This
->lpVtblAR
;
726 IUnknown_AddRef((IUnknown
*)(*ppv
));
730 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
732 return E_NOINTERFACE
;
735 static ULONG WINAPI
FileAsyncReaderPin_Release(IPin
* iface
)
737 ICOM_THIS(FileAsyncReader
, iface
);
741 if (!InterlockedDecrement(&This
->pin
.pin
.refCount
))
743 DATAREQUEST
* pCurrent
;
745 for (pCurrent
= This
->pHead
; pCurrent
; pCurrent
= pNext
)
747 pNext
= pCurrent
->pNext
;
748 CoTaskMemFree(pCurrent
);
750 CloseHandle(This
->hFile
);
751 CloseHandle(This
->hEvent
);
755 return This
->pin
.pin
.refCount
;
758 static HRESULT WINAPI
FileAsyncReaderPin_EnumMediaTypes(IPin
* iface
, IEnumMediaTypes
** ppEnum
)
760 ENUMMEDIADETAILS emd
;
761 ICOM_THIS(FileAsyncReader
, iface
);
763 TRACE("(%p)\n", ppEnum
);
766 emd
.pMediaTypes
= ((AsyncReader
*)This
->pin
.pin
.pinInfo
.pFilter
)->pmt
;
768 return IEnumMediaTypesImpl_Construct(&emd
, ppEnum
);
771 static HRESULT WINAPI
FileAsyncReaderPin_Connect(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
774 ICOM_THIS(OutputPin
, iface
);
776 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
777 dump_AM_MEDIA_TYPE(pmt
);
779 /* If we try to connect to ourself, we will definitely deadlock.
780 * There are other cases where we could deadlock too, but this
781 * catches the obvious case */
782 assert(pReceivePin
!= iface
);
784 EnterCriticalSection(This
->pin
.pCritSec
);
786 /* if we have been a specific type to connect with, then we can either connect
787 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
788 if (pmt
&& !IsEqualGUID(&pmt
->majortype
, &GUID_NULL
) && !IsEqualGUID(&pmt
->subtype
, &GUID_NULL
))
789 hr
= This
->pConnectSpecific(iface
, pReceivePin
, pmt
);
792 /* negotiate media type */
794 IEnumMediaTypes
* pEnumCandidates
;
795 AM_MEDIA_TYPE
* pmtCandidate
; /* Candidate media type */
797 if (SUCCEEDED(IPin_EnumMediaTypes(iface
, &pEnumCandidates
)))
799 hr
= VFW_E_NO_ACCEPTABLE_TYPES
; /* Assume the worst, but set to S_OK if connected successfully */
801 /* try this filter's media types first */
802 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
804 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
805 (This
->pConnectSpecific(iface
, pReceivePin
, pmtCandidate
) == S_OK
))
808 CoTaskMemFree(pmtCandidate
);
811 CoTaskMemFree(pmtCandidate
);
813 IEnumMediaTypes_Release(pEnumCandidates
);
816 /* then try receiver filter's media types */
817 if (hr
!= S_OK
&& SUCCEEDED(IPin_EnumMediaTypes(pReceivePin
, &pEnumCandidates
))) /* if we haven't already connected successfully */
819 while (S_OK
== IEnumMediaTypes_Next(pEnumCandidates
, 1, &pmtCandidate
, NULL
))
821 if (( !pmt
|| CompareMediaTypes(pmt
, pmtCandidate
, TRUE
) ) &&
822 (This
->pConnectSpecific(iface
, pReceivePin
, pmtCandidate
) == S_OK
))
825 CoTaskMemFree(pmtCandidate
);
828 CoTaskMemFree(pmtCandidate
);
830 IEnumMediaTypes_Release(pEnumCandidates
);
832 } /* if negotiate media type */
834 LeaveCriticalSection(This
->pin
.pCritSec
);
836 TRACE("-- %lx\n", hr
);
840 static const IPinVtbl FileAsyncReaderPin_Vtbl
=
842 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
843 FileAsyncReaderPin_QueryInterface
,
845 FileAsyncReaderPin_Release
,
846 FileAsyncReaderPin_Connect
,
847 OutputPin_ReceiveConnection
,
849 IPinImpl_ConnectedTo
,
850 IPinImpl_ConnectionMediaType
,
851 IPinImpl_QueryPinInfo
,
852 IPinImpl_QueryDirection
,
854 IPinImpl_QueryAccept
,
855 FileAsyncReaderPin_EnumMediaTypes
,
856 IPinImpl_QueryInternalConnections
,
857 OutputPin_EndOfStream
,
858 OutputPin_BeginFlush
,
863 static HRESULT
FileAsyncReader_Construct(HANDLE hFile
, IBaseFilter
* pBaseFilter
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
865 FileAsyncReader
* pPinImpl
;
870 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
873 return E_OUTOFMEMORY
;
875 piOutput
.dir
= PINDIR_OUTPUT
;
876 piOutput
.pFilter
= pBaseFilter
;
877 strcpyW(piOutput
.achName
, wszOutputPinName
);
879 if (SUCCEEDED(OutputPin_Init(&piOutput
, pBaseFilter
, AcceptProcAFR
, pCritSec
, &pPinImpl
->pin
)))
881 pPinImpl
->pin
.pin
.lpVtbl
= &FileAsyncReaderPin_Vtbl
;
882 pPinImpl
->lpVtblAR
= &FileAsyncReader_Vtbl
;
883 pPinImpl
->hFile
= hFile
;
884 pPinImpl
->hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
885 pPinImpl
->bFlushing
= FALSE
;
886 pPinImpl
->pHead
= NULL
;
888 *ppPin
= (IPin
*)(&pPinImpl
->pin
.pin
.lpVtbl
);
896 static HRESULT WINAPI
FileAsyncReader_QueryInterface(IAsyncReader
* iface
, REFIID riid
, LPVOID
* ppv
)
898 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
900 return IPin_QueryInterface((IPin
*)This
, riid
, ppv
);
903 static ULONG WINAPI
FileAsyncReader_AddRef(IAsyncReader
* iface
)
905 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
907 return IPin_AddRef((IPin
*)This
);
910 static ULONG WINAPI
FileAsyncReader_Release(IAsyncReader
* iface
)
912 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
914 return IPin_Release((IPin
*)This
);
917 #define DEF_ALIGNMENT 1
919 static HRESULT WINAPI
FileAsyncReader_RequestAllocator(IAsyncReader
* iface
, IMemAllocator
* pPreferred
, ALLOCATOR_PROPERTIES
* pProps
, IMemAllocator
** ppActual
)
923 TRACE("(%p, %p, %p)\n", pPreferred
, pProps
, ppActual
);
925 if (!pProps
->cbAlign
|| (pProps
->cbAlign
% DEF_ALIGNMENT
) != 0)
926 pProps
->cbAlign
= DEF_ALIGNMENT
;
930 ALLOCATOR_PROPERTIES PropsActual
;
931 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, &PropsActual
);
932 /* FIXME: check we are still aligned */
935 IMemAllocator_AddRef(pPreferred
);
936 *ppActual
= pPreferred
;
937 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr
);
944 hr
= CoCreateInstance(&CLSID_MemoryAllocator
, NULL
, CLSCTX_INPROC
, &IID_IMemAllocator
, (LPVOID
*)&pPreferred
);
948 ALLOCATOR_PROPERTIES PropsActual
;
949 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, &PropsActual
);
950 /* FIXME: check we are still aligned */
953 IMemAllocator_AddRef(pPreferred
);
954 *ppActual
= pPreferred
;
955 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr
);
964 IMemAllocator_Release(pPreferred
);
967 TRACE("-- %lx\n", hr
);
971 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
972 * however, this would be quite complicated to do and may be a bit error prone */
973 static HRESULT WINAPI
FileAsyncReader_Request(IAsyncReader
* iface
, IMediaSample
* pSample
, DWORD_PTR dwUser
)
975 REFERENCE_TIME Start
;
977 DATAREQUEST
* pDataRq
;
980 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
982 TRACE("(%p, %lx)\n", pSample
, dwUser
);
984 /* check flushing state */
986 return VFW_E_WRONG_STATE
;
988 if (!(pDataRq
= CoTaskMemAlloc(sizeof(*pDataRq
))))
991 /* get start and stop positions in bytes */
993 hr
= IMediaSample_GetTime(pSample
, &Start
, &Stop
);
996 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
1000 DWORD dwLength
= (DWORD
) BYTES_FROM_MEDIATIME(Stop
- Start
);
1002 pDataRq
->ovl
.Offset
= (DWORD
) BYTES_FROM_MEDIATIME(Start
);
1003 pDataRq
->ovl
.OffsetHigh
= (DWORD
)(BYTES_FROM_MEDIATIME(Start
) >> (sizeof(DWORD
) * 8));
1004 pDataRq
->ovl
.hEvent
= This
->hEvent
;
1005 pDataRq
->dwUserData
= dwUser
;
1006 pDataRq
->pNext
= NULL
;
1007 /* we violate traditional COM rules here by maintaining
1008 * a reference to the sample, but not calling AddRef, but
1009 * that's what MSDN says to do */
1010 pDataRq
->pSample
= pSample
;
1012 EnterCriticalSection(&This
->csList
);
1015 /* adds data request to end of list */
1016 queue(This
->pHead
, pDataRq
);
1018 This
->pHead
= pDataRq
;
1020 LeaveCriticalSection(&This
->csList
);
1022 /* this is definitely not how it is implemented on Win9x
1023 * as they do not support async reads on files, but it is
1024 * sooo much easier to use this than messing around with threads!
1026 if (!ReadFile(This
->hFile
, pBuffer
, dwLength
, NULL
, &pDataRq
->ovl
))
1027 hr
= HRESULT_FROM_WIN32(GetLastError());
1029 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1030 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1034 if (FAILED(hr
) && pDataRq
)
1036 EnterCriticalSection(&This
->csList
);
1038 DATAREQUEST
* pCurrent
;
1039 for (pCurrent
= This
->pHead
; pCurrent
&& pCurrent
->pNext
; pCurrent
= pCurrent
->pNext
)
1040 if (pCurrent
->pNext
== pDataRq
)
1042 pCurrent
->pNext
= pDataRq
->pNext
;
1046 LeaveCriticalSection(&This
->csList
);
1047 CoTaskMemFree(pDataRq
);
1050 TRACE("-- %lx\n", hr
);
1054 static HRESULT WINAPI
FileAsyncReader_WaitForNext(IAsyncReader
* iface
, DWORD dwTimeout
, IMediaSample
** ppSample
, DWORD_PTR
* pdwUser
)
1057 DATAREQUEST
* pDataRq
= NULL
;
1058 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1060 TRACE("(%lu, %p, %p)\n", dwTimeout
, ppSample
, pdwUser
);
1062 /* FIXME: we could do with improving this by waiting for an array of event handles
1063 * and then determining which one finished and removing that from the list, otherwise
1064 * we will end up waiting for longer than we should do, if a later request finishes
1065 * before an earlier one */
1070 /* we return immediately if flushing */
1071 if (This
->bFlushing
)
1072 hr
= VFW_E_WRONG_STATE
;
1076 /* wait for the read to finish or timeout */
1077 if (WaitForSingleObject(This
->hEvent
, dwTimeout
) == WAIT_TIMEOUT
)
1082 EnterCriticalSection(&This
->csList
);
1084 pDataRq
= This
->pHead
;
1085 if (pDataRq
== NULL
)
1088 This
->pHead
= pDataRq
->pNext
;
1090 LeaveCriticalSection(&This
->csList
);
1096 /* get any errors */
1097 if (!GetOverlappedResult(This
->hFile
, &pDataRq
->ovl
, &dwBytes
, TRUE
))
1098 hr
= HRESULT_FROM_WIN32(GetLastError());
1103 *ppSample
= pDataRq
->pSample
;
1104 *pdwUser
= pDataRq
->dwUserData
;
1110 /* no need to close event handle since we will close it when the pin is destroyed */
1111 CoTaskMemFree(pDataRq
);
1114 TRACE("-- %lx\n", hr
);
1118 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
);
1120 static HRESULT WINAPI
FileAsyncReader_SyncReadAligned(IAsyncReader
* iface
, IMediaSample
* pSample
)
1123 REFERENCE_TIME tStart
;
1124 REFERENCE_TIME tStop
;
1127 TRACE("(%p)\n", pSample
);
1129 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
1132 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
1135 hr
= FileAsyncReader_SyncRead(iface
,
1136 BYTES_FROM_MEDIATIME(tStart
),
1137 (LONG
) BYTES_FROM_MEDIATIME(tStop
- tStart
),
1140 TRACE("-- %lx\n", hr
);
1144 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
)
1148 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1150 TRACE("(%lx%08lx, %ld, %p)\n", (ULONG
)(llPosition
>> 32), (ULONG
)llPosition
, lLength
, pBuffer
);
1152 ZeroMemory(&ovl
, sizeof(ovl
));
1154 ovl
.hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
1155 /* NOTE: llPosition is the actual byte position to start reading from */
1156 ovl
.Offset
= (DWORD
) llPosition
;
1157 ovl
.OffsetHigh
= (DWORD
) (llPosition
>> (sizeof(DWORD
) * 8));
1159 if (!ReadFile(This
->hFile
, pBuffer
, lLength
, NULL
, &ovl
))
1160 hr
= HRESULT_FROM_WIN32(GetLastError());
1162 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1169 if (!GetOverlappedResult(This
->hFile
, &ovl
, &dwBytesRead
, TRUE
))
1170 hr
= HRESULT_FROM_WIN32(GetLastError());
1173 CloseHandle(ovl
.hEvent
);
1175 TRACE("-- %lx\n", hr
);
1179 static HRESULT WINAPI
FileAsyncReader_Length(IAsyncReader
* iface
, LONGLONG
* pTotal
, LONGLONG
* pAvailable
)
1183 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1185 TRACE("(%p, %p)\n", pTotal
, pAvailable
);
1187 if (((dwSizeLow
= GetFileSize(This
->hFile
, &dwSizeHigh
)) == -1) &&
1188 (GetLastError() != NO_ERROR
))
1189 return HRESULT_FROM_WIN32(GetLastError());
1191 *pTotal
= (LONGLONG
)dwSizeLow
| (LONGLONG
)dwSizeHigh
<< (sizeof(DWORD
) * 8);
1193 *pAvailable
= *pTotal
;
1198 static HRESULT WINAPI
FileAsyncReader_BeginFlush(IAsyncReader
* iface
)
1200 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1204 This
->bFlushing
= TRUE
;
1205 CancelIo(This
->hFile
);
1206 SetEvent(This
->hEvent
);
1208 /* FIXME: free list */
1213 static HRESULT WINAPI
FileAsyncReader_EndFlush(IAsyncReader
* iface
)
1215 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1219 This
->bFlushing
= FALSE
;
1224 static const IAsyncReaderVtbl FileAsyncReader_Vtbl
=
1226 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1227 FileAsyncReader_QueryInterface
,
1228 FileAsyncReader_AddRef
,
1229 FileAsyncReader_Release
,
1230 FileAsyncReader_RequestAllocator
,
1231 FileAsyncReader_Request
,
1232 FileAsyncReader_WaitForNext
,
1233 FileAsyncReader_SyncReadAligned
,
1234 FileAsyncReader_SyncRead
,
1235 FileAsyncReader_Length
,
1236 FileAsyncReader_BeginFlush
,
1237 FileAsyncReader_EndFlush
,