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"
35 WINE_DEFAULT_DEBUG_CHANNEL(quartz
);
37 static const WCHAR wszOutputPinName
[] = { 'O','u','t','p','u','t',0 };
39 typedef struct AsyncReader
41 const IBaseFilterVtbl
* lpVtbl
;
42 const IFileSourceFilterVtbl
* lpVtblFSF
;
45 FILTER_INFO filterInfo
;
47 CRITICAL_SECTION csFilter
;
54 static const IBaseFilterVtbl AsyncReader_Vtbl
;
55 static const IFileSourceFilterVtbl FileSource_Vtbl
;
56 static const IAsyncReaderVtbl FileAsyncReader_Vtbl
;
58 static HRESULT
FileAsyncReader_Construct(HANDLE hFile
, IBaseFilter
* pBaseFilter
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
);
60 static inline AsyncReader
*impl_from_IFileSourceFilter( IFileSourceFilter
*iface
)
62 return (AsyncReader
*)((char*)iface
- FIELD_OFFSET(AsyncReader
, lpVtblFSF
));
65 static HRESULT
process_extensions(HKEY hkeyExtensions
, LPCOLESTR pszFileName
, GUID
* majorType
, GUID
* minorType
)
67 /* FIXME: implement */
71 static unsigned char byte_from_hex_char(WCHAR wHex
)
73 switch (tolowerW(wHex
))
85 return (wHex
- '0') & 0xf;
92 return (wHex
- 'a' + 10) & 0xf;
98 static HRESULT
process_pattern_string(LPCWSTR wszPatternString
, IAsyncReader
* pReader
)
108 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString
));
110 /* format: "offset, bytestocompare, mask, value" */
112 ulOffset
= strtolW(wszPatternString
, NULL
, 10);
114 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
117 wszPatternString
++; /* skip ',' */
119 ulBytes
= strtolW(wszPatternString
, NULL
, 10);
121 pbMask
= HeapAlloc(GetProcessHeap(), 0, ulBytes
);
122 pbValue
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, ulBytes
);
123 pbFile
= HeapAlloc(GetProcessHeap(), 0, ulBytes
);
125 /* default mask is match everything */
126 memset(pbMask
, 0xFF, ulBytes
);
128 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
131 wszPatternString
++; /* skip ',' */
135 for ( ; !isxdigitW(*wszPatternString
) && (*wszPatternString
!= ','); wszPatternString
++)
138 for (strpos
= 0; isxdigitW(*wszPatternString
) && (strpos
/2 < ulBytes
); wszPatternString
++, strpos
++)
140 if ((strpos
% 2) == 1) /* odd numbered position */
141 pbMask
[strpos
/ 2] |= byte_from_hex_char(*wszPatternString
);
143 pbMask
[strpos
/ 2] = byte_from_hex_char(*wszPatternString
) << 4;
146 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
149 wszPatternString
++; /* skip ',' */
154 for ( ; !isxdigitW(*wszPatternString
) && (*wszPatternString
!= ','); wszPatternString
++)
157 for (strpos
= 0; isxdigitW(*wszPatternString
) && (strpos
/2 < ulBytes
); wszPatternString
++, strpos
++)
159 if ((strpos
% 2) == 1) /* odd numbered position */
160 pbValue
[strpos
/ 2] |= byte_from_hex_char(*wszPatternString
);
162 pbValue
[strpos
/ 2] = byte_from_hex_char(*wszPatternString
) << 4;
167 hr
= IAsyncReader_SyncRead(pReader
, ulOffset
, ulBytes
, pbFile
);
172 for (i
= 0; i
< ulBytes
; i
++)
173 if ((pbFile
[i
] & pbMask
[i
]) != pbValue
[i
])
180 HeapFree(GetProcessHeap(), 0, pbMask
);
181 HeapFree(GetProcessHeap(), 0, pbValue
);
182 HeapFree(GetProcessHeap(), 0, pbFile
);
184 /* if we encountered no errors with this string, and there is a following tuple, then we
185 * have to match that as well to succeed */
186 if ((hr
== S_OK
) && (wszPatternString
= strchrW(wszPatternString
, ',')))
187 return process_pattern_string(wszPatternString
+ 1, pReader
);
192 static HRESULT
GetClassMediaFile(IAsyncReader
* pReader
, LPCOLESTR pszFileName
, GUID
* majorType
, GUID
* minorType
)
194 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 lRet
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, wszMediaType
, 0, KEY_READ
, &hkeyMediaType
);
204 hr
= HRESULT_FROM_WIN32(lRet
);
210 for (indexMajor
= 0; !bFound
; indexMajor
++)
213 WCHAR wszMajorKeyName
[CHARS_IN_GUID
];
214 DWORD dwKeyNameLength
= sizeof(wszMajorKeyName
) / sizeof(wszMajorKeyName
[0]);
215 static const WCHAR wszExtensions
[] = {'E','x','t','e','n','s','i','o','n','s',0};
217 if (RegEnumKeyExW(hkeyMediaType
, indexMajor
, wszMajorKeyName
, &dwKeyNameLength
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
219 if (RegOpenKeyExW(hkeyMediaType
, wszMajorKeyName
, 0, KEY_READ
, &hkeyMajor
) != ERROR_SUCCESS
)
221 TRACE("%s\n", debugstr_w(wszMajorKeyName
));
222 if (!strcmpW(wszExtensions
, wszMajorKeyName
))
224 if (process_extensions(hkeyMajor
, pszFileName
, majorType
, minorType
) == S_OK
)
231 for (indexMinor
= 0; !bFound
; indexMinor
++)
234 WCHAR wszMinorKeyName
[CHARS_IN_GUID
];
235 DWORD dwMinorKeyNameLen
= sizeof(wszMinorKeyName
) / sizeof(wszMinorKeyName
[0]);
239 if (RegEnumKeyExW(hkeyMajor
, indexMinor
, wszMinorKeyName
, &dwMinorKeyNameLen
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
242 if (RegOpenKeyExW(hkeyMajor
, wszMinorKeyName
, 0, KEY_READ
, &hkeyMinor
) != ERROR_SUCCESS
)
245 TRACE("\t%s\n", debugstr_w(wszMinorKeyName
));
247 if (RegQueryInfoKeyW(hkeyMinor
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &maxValueLen
, NULL
, NULL
) != ERROR_SUCCESS
)
250 for (indexValue
= 0; !bFound
; indexValue
++)
253 WCHAR wszValueName
[14]; /* longest name we should encounter will be "Source Filter" */
254 LPWSTR wszPatternString
= HeapAlloc(GetProcessHeap(), 0, maxValueLen
);
255 DWORD dwValueNameLen
= sizeof(wszValueName
) / sizeof(wszValueName
[0]); /* remember this is in chars */
256 DWORD dwDataLen
= maxValueLen
; /* remember this is in bytes */
257 static const WCHAR wszSourceFilter
[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
260 if ((temp
= RegEnumValueW(hkeyMinor
, indexValue
, wszValueName
, &dwValueNameLen
, NULL
, &dwType
, (LPBYTE
)wszPatternString
, &dwDataLen
)) != ERROR_SUCCESS
)
262 HeapFree(GetProcessHeap(), 0, wszPatternString
);
266 /* if it is not the source filter value */
267 if (strcmpW(wszValueName
, wszSourceFilter
))
269 if (process_pattern_string(wszPatternString
, pReader
) == S_OK
)
271 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName
, majorType
)) &&
272 SUCCEEDED(CLSIDFromString(wszMinorKeyName
, minorType
)))
276 HeapFree(GetProcessHeap(), 0, wszPatternString
);
278 CloseHandle(hkeyMinor
);
281 CloseHandle(hkeyMajor
);
284 CloseHandle(hkeyMediaType
);
286 if (SUCCEEDED(hr
) && !bFound
)
288 ERR("Media class not found\n");
292 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType
), qzdebugstr_guid(minorType
));
297 HRESULT
AsyncReader_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
299 AsyncReader
*pAsyncRead
;
302 return CLASS_E_NOAGGREGATION
;
304 pAsyncRead
= CoTaskMemAlloc(sizeof(AsyncReader
));
307 return E_OUTOFMEMORY
;
309 pAsyncRead
->lpVtbl
= &AsyncReader_Vtbl
;
310 pAsyncRead
->lpVtblFSF
= &FileSource_Vtbl
;
311 pAsyncRead
->refCount
= 1;
312 pAsyncRead
->filterInfo
.achName
[0] = '\0';
313 pAsyncRead
->filterInfo
.pGraph
= NULL
;
314 pAsyncRead
->pOutputPin
= NULL
;
316 InitializeCriticalSection(&pAsyncRead
->csFilter
);
317 pAsyncRead
->csFilter
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": AsyncReader.csFilter");
319 pAsyncRead
->pszFileName
= NULL
;
320 pAsyncRead
->pmt
= NULL
;
322 *ppv
= (LPVOID
)pAsyncRead
;
324 TRACE("-- created at %p\n", pAsyncRead
);
329 /** IUnknown methods **/
331 static HRESULT WINAPI
AsyncReader_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
333 AsyncReader
*This
= (AsyncReader
*)iface
;
335 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
339 if (IsEqualIID(riid
, &IID_IUnknown
))
341 else if (IsEqualIID(riid
, &IID_IPersist
))
343 else if (IsEqualIID(riid
, &IID_IMediaFilter
))
345 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
347 else if (IsEqualIID(riid
, &IID_IFileSourceFilter
))
348 *ppv
= (LPVOID
)(&This
->lpVtblFSF
);
352 IUnknown_AddRef((IUnknown
*)(*ppv
));
356 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
358 return E_NOINTERFACE
;
361 static ULONG WINAPI
AsyncReader_AddRef(IBaseFilter
* iface
)
363 AsyncReader
*This
= (AsyncReader
*)iface
;
364 ULONG refCount
= InterlockedIncrement(&This
->refCount
);
366 TRACE("(%p)->() AddRef from %d\n", This
, refCount
- 1);
371 static ULONG WINAPI
AsyncReader_Release(IBaseFilter
* iface
)
373 AsyncReader
*This
= (AsyncReader
*)iface
;
374 ULONG refCount
= InterlockedDecrement(&This
->refCount
);
376 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
380 if (This
->pOutputPin
)
383 if(SUCCEEDED(IPin_ConnectedTo(This
->pOutputPin
, &pConnectedTo
)))
385 IPin_Disconnect(pConnectedTo
);
386 IPin_Release(pConnectedTo
);
388 IPin_Disconnect(This
->pOutputPin
);
389 IPin_Release(This
->pOutputPin
);
391 This
->csFilter
.DebugInfo
->Spare
[0] = 0;
392 DeleteCriticalSection(&This
->csFilter
);
401 /** IPersist methods **/
403 static HRESULT WINAPI
AsyncReader_GetClassID(IBaseFilter
* iface
, CLSID
* pClsid
)
405 TRACE("(%p)\n", pClsid
);
407 *pClsid
= CLSID_AsyncReader
;
412 /** IMediaFilter methods **/
414 static HRESULT WINAPI
AsyncReader_Stop(IBaseFilter
* iface
)
416 AsyncReader
*This
= (AsyncReader
*)iface
;
420 This
->state
= State_Stopped
;
425 static HRESULT WINAPI
AsyncReader_Pause(IBaseFilter
* iface
)
427 AsyncReader
*This
= (AsyncReader
*)iface
;
431 This
->state
= State_Paused
;
436 static HRESULT WINAPI
AsyncReader_Run(IBaseFilter
* iface
, REFERENCE_TIME tStart
)
438 AsyncReader
*This
= (AsyncReader
*)iface
;
440 TRACE("(%x%08x)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
);
442 This
->state
= State_Running
;
447 static HRESULT WINAPI
AsyncReader_GetState(IBaseFilter
* iface
, DWORD dwMilliSecsTimeout
, FILTER_STATE
*pState
)
449 AsyncReader
*This
= (AsyncReader
*)iface
;
451 TRACE("(%u, %p)\n", dwMilliSecsTimeout
, pState
);
453 *pState
= This
->state
;
458 static HRESULT WINAPI
AsyncReader_SetSyncSource(IBaseFilter
* iface
, IReferenceClock
*pClock
)
460 /* AsyncReader *This = (AsyncReader *)iface;*/
462 TRACE("(%p)\n", pClock
);
467 static HRESULT WINAPI
AsyncReader_GetSyncSource(IBaseFilter
* iface
, IReferenceClock
**ppClock
)
469 /* AsyncReader *This = (AsyncReader *)iface;*/
471 TRACE("(%p)\n", ppClock
);
476 /** IBaseFilter methods **/
478 static HRESULT WINAPI
AsyncReader_EnumPins(IBaseFilter
* iface
, IEnumPins
**ppEnum
)
481 AsyncReader
*This
= (AsyncReader
*)iface
;
483 TRACE("(%p)\n", ppEnum
);
485 epd
.cPins
= This
->pOutputPin
? 1 : 0;
486 epd
.ppPins
= &This
->pOutputPin
;
487 return IEnumPinsImpl_Construct(&epd
, ppEnum
);
490 static HRESULT WINAPI
AsyncReader_FindPin(IBaseFilter
* iface
, LPCWSTR Id
, IPin
**ppPin
)
492 FIXME("(%s, %p)\n", debugstr_w(Id
), ppPin
);
497 static HRESULT WINAPI
AsyncReader_QueryFilterInfo(IBaseFilter
* iface
, FILTER_INFO
*pInfo
)
499 AsyncReader
*This
= (AsyncReader
*)iface
;
501 TRACE("(%p)\n", pInfo
);
503 strcpyW(pInfo
->achName
, This
->filterInfo
.achName
);
504 pInfo
->pGraph
= This
->filterInfo
.pGraph
;
507 IFilterGraph_AddRef(pInfo
->pGraph
);
512 static HRESULT WINAPI
AsyncReader_JoinFilterGraph(IBaseFilter
* iface
, IFilterGraph
*pGraph
, LPCWSTR pName
)
514 AsyncReader
*This
= (AsyncReader
*)iface
;
516 TRACE("(%p, %s)\n", pGraph
, debugstr_w(pName
));
519 strcpyW(This
->filterInfo
.achName
, pName
);
521 *This
->filterInfo
.achName
= 0;
522 This
->filterInfo
.pGraph
= pGraph
; /* NOTE: do NOT increase ref. count */
527 static HRESULT WINAPI
AsyncReader_QueryVendorInfo(IBaseFilter
* iface
, LPWSTR
*pVendorInfo
)
529 FIXME("(%p)\n", pVendorInfo
);
534 static const IBaseFilterVtbl AsyncReader_Vtbl
=
536 AsyncReader_QueryInterface
,
539 AsyncReader_GetClassID
,
543 AsyncReader_GetState
,
544 AsyncReader_SetSyncSource
,
545 AsyncReader_GetSyncSource
,
546 AsyncReader_EnumPins
,
548 AsyncReader_QueryFilterInfo
,
549 AsyncReader_JoinFilterGraph
,
550 AsyncReader_QueryVendorInfo
553 static HRESULT WINAPI
FileSource_QueryInterface(IFileSourceFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
555 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
557 return IBaseFilter_QueryInterface((IFileSourceFilter
*)&This
->lpVtbl
, riid
, ppv
);
560 static ULONG WINAPI
FileSource_AddRef(IFileSourceFilter
* iface
)
562 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
564 return IBaseFilter_AddRef((IFileSourceFilter
*)&This
->lpVtbl
);
567 static ULONG WINAPI
FileSource_Release(IFileSourceFilter
* iface
)
569 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
571 return IBaseFilter_Release((IFileSourceFilter
*)&This
->lpVtbl
);
574 static HRESULT WINAPI
FileSource_Load(IFileSourceFilter
* iface
, LPCOLESTR pszFileName
, const AM_MEDIA_TYPE
* pmt
)
578 IAsyncReader
* pReader
= NULL
;
579 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
581 TRACE("(%s, %p)\n", debugstr_w(pszFileName
), pmt
);
584 /* FIXME: check the sharing values that native uses */
585 hFile
= CreateFileW(pszFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, NULL
);
587 if (hFile
== INVALID_HANDLE_VALUE
)
589 return HRESULT_FROM_WIN32(GetLastError());
593 hr
= FileAsyncReader_Construct(hFile
, (IBaseFilter
*)&This
->lpVtbl
, &This
->csFilter
, &This
->pOutputPin
);
596 hr
= IPin_QueryInterface(This
->pOutputPin
, &IID_IAsyncReader
, (LPVOID
*)&pReader
);
598 /* store file name & media type */
601 This
->pszFileName
= CoTaskMemAlloc((strlenW(pszFileName
) + 1) * sizeof(WCHAR
));
602 strcpyW(This
->pszFileName
, pszFileName
);
603 This
->pmt
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
));
606 This
->pmt
->bFixedSizeSamples
= TRUE
;
607 This
->pmt
->bTemporalCompression
= FALSE
;
608 This
->pmt
->cbFormat
= 0;
609 This
->pmt
->pbFormat
= NULL
;
610 This
->pmt
->pUnk
= NULL
;
611 This
->pmt
->lSampleSize
= 0;
612 memcpy(&This
->pmt
->formattype
, &FORMAT_None
, sizeof(FORMAT_None
));
613 hr
= GetClassMediaFile(pReader
, pszFileName
, &This
->pmt
->majortype
, &This
->pmt
->subtype
);
616 CoTaskMemFree(This
->pmt
);
621 CopyMediaType(This
->pmt
, pmt
);
625 IAsyncReader_Release(pReader
);
629 if (This
->pOutputPin
)
631 IPin_Release(This
->pOutputPin
);
632 This
->pOutputPin
= NULL
;
635 CoTaskMemFree(This
->pszFileName
);
636 This
->pszFileName
= NULL
;
641 /* FIXME: check return codes */
645 static HRESULT WINAPI
FileSource_GetCurFile(IFileSourceFilter
* iface
, LPOLESTR
* ppszFileName
, AM_MEDIA_TYPE
* pmt
)
647 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
649 TRACE("(%p, %p)\n", ppszFileName
, pmt
);
654 /* copy file name & media type if available, otherwise clear the outputs */
655 if (This
->pszFileName
)
657 *ppszFileName
= CoTaskMemAlloc((strlenW(This
->pszFileName
) + 1) * sizeof(WCHAR
));
658 strcpyW(*ppszFileName
, This
->pszFileName
);
661 *ppszFileName
= NULL
;
666 CopyMediaType(pmt
, This
->pmt
);
668 ZeroMemory(pmt
, sizeof(*pmt
));
674 static const IFileSourceFilterVtbl FileSource_Vtbl
=
676 FileSource_QueryInterface
,
680 FileSource_GetCurFile
684 /* the dwUserData passed back to user */
685 typedef struct DATAREQUEST
687 IMediaSample
* pSample
; /* sample passed to us by user */
688 DWORD_PTR dwUserData
; /* user data passed to us */
689 OVERLAPPED ovl
; /* our overlapped structure */
691 struct DATAREQUEST
* pNext
; /* next data request in list */
694 static void queue(DATAREQUEST
* pHead
, DATAREQUEST
* pItem
)
696 DATAREQUEST
* pCurrent
;
697 for (pCurrent
= pHead
; pCurrent
->pNext
; pCurrent
= pCurrent
->pNext
)
699 pCurrent
->pNext
= pItem
;
702 typedef struct FileAsyncReader
705 const struct IAsyncReaderVtbl
* lpVtblAR
;
710 DATAREQUEST
* pHead
; /* head of data request list */
711 CRITICAL_SECTION csList
; /* critical section to protect operations on list */
714 static inline FileAsyncReader
*impl_from_IAsyncReader( IAsyncReader
*iface
)
716 return (FileAsyncReader
*)((char*)iface
- FIELD_OFFSET(FileAsyncReader
, lpVtblAR
));
719 static HRESULT
AcceptProcAFR(LPVOID iface
, const AM_MEDIA_TYPE
*pmt
)
721 AsyncReader
*This
= (AsyncReader
*)iface
;
723 FIXME("(%p, %p)\n", iface
, pmt
);
725 if (IsEqualGUID(&pmt
->majortype
, &This
->pmt
->majortype
) &&
726 IsEqualGUID(&pmt
->subtype
, &This
->pmt
->subtype
) &&
727 IsEqualGUID(&pmt
->formattype
, &FORMAT_None
))
733 /* overriden pin functions */
735 static HRESULT WINAPI
FileAsyncReaderPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
737 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
738 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
742 if (IsEqualIID(riid
, &IID_IUnknown
))
744 else if (IsEqualIID(riid
, &IID_IPin
))
746 else if (IsEqualIID(riid
, &IID_IAsyncReader
))
747 *ppv
= (LPVOID
)&This
->lpVtblAR
;
751 IUnknown_AddRef((IUnknown
*)(*ppv
));
755 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
757 return E_NOINTERFACE
;
760 static ULONG WINAPI
FileAsyncReaderPin_Release(IPin
* iface
)
762 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
763 ULONG refCount
= InterlockedDecrement(&This
->pin
.pin
.refCount
);
765 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
769 DATAREQUEST
* pCurrent
;
771 for (pCurrent
= This
->pHead
; pCurrent
; pCurrent
= pNext
)
773 pNext
= pCurrent
->pNext
;
774 CoTaskMemFree(pCurrent
);
776 CloseHandle(This
->hFile
);
777 CloseHandle(This
->hEvent
);
778 This
->csList
.DebugInfo
->Spare
[0] = 0;
779 DeleteCriticalSection(&This
->csList
);
786 static HRESULT WINAPI
FileAsyncReaderPin_EnumMediaTypes(IPin
* iface
, IEnumMediaTypes
** ppEnum
)
788 ENUMMEDIADETAILS emd
;
789 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
791 TRACE("(%p)\n", ppEnum
);
794 emd
.pMediaTypes
= ((AsyncReader
*)This
->pin
.pin
.pinInfo
.pFilter
)->pmt
;
796 return IEnumMediaTypesImpl_Construct(&emd
, ppEnum
);
799 static const IPinVtbl FileAsyncReaderPin_Vtbl
=
801 FileAsyncReaderPin_QueryInterface
,
803 FileAsyncReaderPin_Release
,
805 OutputPin_ReceiveConnection
,
807 IPinImpl_ConnectedTo
,
808 IPinImpl_ConnectionMediaType
,
809 IPinImpl_QueryPinInfo
,
810 IPinImpl_QueryDirection
,
812 IPinImpl_QueryAccept
,
813 FileAsyncReaderPin_EnumMediaTypes
,
814 IPinImpl_QueryInternalConnections
,
815 OutputPin_EndOfStream
,
816 OutputPin_BeginFlush
,
821 /* Function called as a helper to IPin_Connect */
822 /* specific AM_MEDIA_TYPE - it cannot be NULL */
823 /* this differs from standard OutputPin_ConnectSpecific only in that it
824 * doesn't need the IMemInputPin interface on the receiving pin */
825 static HRESULT
FileAsyncReaderPin_ConnectSpecific(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
827 OutputPin
*This
= (OutputPin
*)iface
;
830 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
831 dump_AM_MEDIA_TYPE(pmt
);
833 /* FIXME: call queryacceptproc */
835 This
->pin
.pConnectedTo
= pReceivePin
;
836 IPin_AddRef(pReceivePin
);
837 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
839 hr
= IPin_ReceiveConnection(pReceivePin
, iface
, pmt
);
843 IPin_Release(This
->pin
.pConnectedTo
);
844 This
->pin
.pConnectedTo
= NULL
;
845 FreeMediaType(&This
->pin
.mtCurrent
);
848 TRACE(" -- %x\n", hr
);
852 static HRESULT
FileAsyncReader_Construct(HANDLE hFile
, IBaseFilter
* pBaseFilter
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
854 FileAsyncReader
* pPinImpl
;
859 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
862 return E_OUTOFMEMORY
;
864 piOutput
.dir
= PINDIR_OUTPUT
;
865 piOutput
.pFilter
= pBaseFilter
;
866 strcpyW(piOutput
.achName
, wszOutputPinName
);
868 if (SUCCEEDED(OutputPin_Init(&piOutput
, NULL
, pBaseFilter
, AcceptProcAFR
, pCritSec
, &pPinImpl
->pin
)))
870 pPinImpl
->pin
.pin
.lpVtbl
= &FileAsyncReaderPin_Vtbl
;
871 pPinImpl
->lpVtblAR
= &FileAsyncReader_Vtbl
;
872 pPinImpl
->hFile
= hFile
;
873 pPinImpl
->hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
874 pPinImpl
->bFlushing
= FALSE
;
875 pPinImpl
->pHead
= NULL
;
876 pPinImpl
->pin
.pConnectSpecific
= FileAsyncReaderPin_ConnectSpecific
;
877 InitializeCriticalSection(&pPinImpl
->csList
);
878 pPinImpl
->csList
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FileAsyncReader.csList");
880 *ppPin
= (IPin
*)(&pPinImpl
->pin
.pin
.lpVtbl
);
888 static HRESULT WINAPI
FileAsyncReader_QueryInterface(IAsyncReader
* iface
, REFIID riid
, LPVOID
* ppv
)
890 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
892 return IPin_QueryInterface((IPin
*)This
, riid
, ppv
);
895 static ULONG WINAPI
FileAsyncReader_AddRef(IAsyncReader
* iface
)
897 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
899 return IPin_AddRef((IPin
*)This
);
902 static ULONG WINAPI
FileAsyncReader_Release(IAsyncReader
* iface
)
904 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
906 return IPin_Release((IPin
*)This
);
909 #define DEF_ALIGNMENT 1
911 static HRESULT WINAPI
FileAsyncReader_RequestAllocator(IAsyncReader
* iface
, IMemAllocator
* pPreferred
, ALLOCATOR_PROPERTIES
* pProps
, IMemAllocator
** ppActual
)
915 TRACE("(%p, %p, %p)\n", pPreferred
, pProps
, ppActual
);
917 if (!pProps
->cbAlign
|| (pProps
->cbAlign
% DEF_ALIGNMENT
) != 0)
918 pProps
->cbAlign
= DEF_ALIGNMENT
;
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 -- %x\n", hr
);
936 hr
= CoCreateInstance(&CLSID_MemoryAllocator
, NULL
, CLSCTX_INPROC
, &IID_IMemAllocator
, (LPVOID
*)&pPreferred
);
940 ALLOCATOR_PROPERTIES PropsActual
;
941 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, &PropsActual
);
942 /* FIXME: check we are still aligned */
945 *ppActual
= pPreferred
;
946 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr
);
955 IMemAllocator_Release(pPreferred
);
958 TRACE("-- %x\n", hr
);
962 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
963 * however, this would be quite complicated to do and may be a bit error prone */
964 static HRESULT WINAPI
FileAsyncReader_Request(IAsyncReader
* iface
, IMediaSample
* pSample
, DWORD_PTR dwUser
)
966 REFERENCE_TIME Start
;
968 DATAREQUEST
* pDataRq
;
971 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
973 TRACE("(%p, %lx)\n", pSample
, dwUser
);
975 /* check flushing state */
977 return VFW_E_WRONG_STATE
;
979 if (!(pDataRq
= CoTaskMemAlloc(sizeof(*pDataRq
))))
982 /* get start and stop positions in bytes */
984 hr
= IMediaSample_GetTime(pSample
, &Start
, &Stop
);
987 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
991 DWORD dwLength
= (DWORD
) BYTES_FROM_MEDIATIME(Stop
- Start
);
993 pDataRq
->ovl
.u
.s
.Offset
= (DWORD
) BYTES_FROM_MEDIATIME(Start
);
994 pDataRq
->ovl
.u
.s
.OffsetHigh
= (DWORD
)(BYTES_FROM_MEDIATIME(Start
) >> (sizeof(DWORD
) * 8));
995 pDataRq
->ovl
.hEvent
= This
->hEvent
;
996 pDataRq
->dwUserData
= dwUser
;
997 pDataRq
->pNext
= NULL
;
998 /* we violate traditional COM rules here by maintaining
999 * a reference to the sample, but not calling AddRef, but
1000 * that's what MSDN says to do */
1001 pDataRq
->pSample
= pSample
;
1003 EnterCriticalSection(&This
->csList
);
1006 /* adds data request to end of list */
1007 queue(This
->pHead
, pDataRq
);
1009 This
->pHead
= pDataRq
;
1011 LeaveCriticalSection(&This
->csList
);
1013 /* this is definitely not how it is implemented on Win9x
1014 * as they do not support async reads on files, but it is
1015 * sooo much easier to use this than messing around with threads!
1017 if (!ReadFile(This
->hFile
, pBuffer
, dwLength
, NULL
, &pDataRq
->ovl
))
1018 hr
= HRESULT_FROM_WIN32(GetLastError());
1020 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1021 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1025 if (FAILED(hr
) && pDataRq
)
1027 EnterCriticalSection(&This
->csList
);
1029 DATAREQUEST
* pCurrent
;
1030 for (pCurrent
= This
->pHead
; pCurrent
&& pCurrent
->pNext
; pCurrent
= pCurrent
->pNext
)
1031 if (pCurrent
->pNext
== pDataRq
)
1033 pCurrent
->pNext
= pDataRq
->pNext
;
1037 LeaveCriticalSection(&This
->csList
);
1038 CoTaskMemFree(pDataRq
);
1041 TRACE("-- %x\n", hr
);
1045 static HRESULT WINAPI
FileAsyncReader_WaitForNext(IAsyncReader
* iface
, DWORD dwTimeout
, IMediaSample
** ppSample
, DWORD_PTR
* pdwUser
)
1049 DATAREQUEST
* pDataRq
= NULL
;
1050 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1052 TRACE("(%u, %p, %p)\n", dwTimeout
, ppSample
, pdwUser
);
1054 /* FIXME: we could do with improving this by waiting for an array of event handles
1055 * and then determining which one finished and removing that from the list, otherwise
1056 * we will end up waiting for longer than we should do, if a later request finishes
1057 * before an earlier one */
1062 /* we return immediately if flushing */
1063 if (This
->bFlushing
)
1064 hr
= VFW_E_WRONG_STATE
;
1068 /* wait for the read to finish or timeout */
1069 if (WaitForSingleObject(This
->hEvent
, dwTimeout
) == WAIT_TIMEOUT
)
1074 EnterCriticalSection(&This
->csList
);
1076 pDataRq
= This
->pHead
;
1077 if (pDataRq
== NULL
)
1080 This
->pHead
= pDataRq
->pNext
;
1082 LeaveCriticalSection(&This
->csList
);
1087 /* get any errors */
1088 if (!GetOverlappedResult(This
->hFile
, &pDataRq
->ovl
, &dwBytes
, FALSE
))
1089 hr
= HRESULT_FROM_WIN32(GetLastError());
1094 IMediaSample_SetActualDataLength(pDataRq
->pSample
, dwBytes
);
1095 *ppSample
= pDataRq
->pSample
;
1096 *pdwUser
= pDataRq
->dwUserData
;
1099 /* no need to close event handle since we will close it when the pin is destroyed */
1100 CoTaskMemFree(pDataRq
);
1102 TRACE("-- %x\n", hr
);
1106 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
);
1108 static HRESULT WINAPI
FileAsyncReader_SyncReadAligned(IAsyncReader
* iface
, IMediaSample
* pSample
)
1111 REFERENCE_TIME tStart
;
1112 REFERENCE_TIME tStop
;
1115 TRACE("(%p)\n", pSample
);
1117 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
1120 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
1123 hr
= FileAsyncReader_SyncRead(iface
,
1124 BYTES_FROM_MEDIATIME(tStart
),
1125 (LONG
) BYTES_FROM_MEDIATIME(tStop
- tStart
),
1128 TRACE("-- %x\n", hr
);
1132 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
)
1136 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1138 TRACE("(%x%08x, %d, %p)\n", (ULONG
)(llPosition
>> 32), (ULONG
)llPosition
, lLength
, pBuffer
);
1140 ZeroMemory(&ovl
, sizeof(ovl
));
1142 ovl
.hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
1143 /* NOTE: llPosition is the actual byte position to start reading from */
1144 ovl
.u
.s
.Offset
= (DWORD
) llPosition
;
1145 ovl
.u
.s
.OffsetHigh
= (DWORD
) (llPosition
>> (sizeof(DWORD
) * 8));
1147 if (!ReadFile(This
->hFile
, pBuffer
, lLength
, NULL
, &ovl
))
1148 hr
= HRESULT_FROM_WIN32(GetLastError());
1150 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1157 if (!GetOverlappedResult(This
->hFile
, &ovl
, &dwBytesRead
, TRUE
))
1158 hr
= HRESULT_FROM_WIN32(GetLastError());
1161 CloseHandle(ovl
.hEvent
);
1163 TRACE("-- %x\n", hr
);
1167 static HRESULT WINAPI
FileAsyncReader_Length(IAsyncReader
* iface
, LONGLONG
* pTotal
, LONGLONG
* pAvailable
)
1171 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1173 TRACE("(%p, %p)\n", pTotal
, pAvailable
);
1175 if (((dwSizeLow
= GetFileSize(This
->hFile
, &dwSizeHigh
)) == -1) &&
1176 (GetLastError() != NO_ERROR
))
1177 return HRESULT_FROM_WIN32(GetLastError());
1179 *pTotal
= (LONGLONG
)dwSizeLow
| (LONGLONG
)dwSizeHigh
<< (sizeof(DWORD
) * 8);
1181 *pAvailable
= *pTotal
;
1186 static HRESULT WINAPI
FileAsyncReader_BeginFlush(IAsyncReader
* iface
)
1188 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1192 This
->bFlushing
= TRUE
;
1193 CancelIo(This
->hFile
);
1194 SetEvent(This
->hEvent
);
1196 /* FIXME: free list */
1201 static HRESULT WINAPI
FileAsyncReader_EndFlush(IAsyncReader
* iface
)
1203 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1207 This
->bFlushing
= FALSE
;
1212 static const IAsyncReaderVtbl FileAsyncReader_Vtbl
=
1214 FileAsyncReader_QueryInterface
,
1215 FileAsyncReader_AddRef
,
1216 FileAsyncReader_Release
,
1217 FileAsyncReader_RequestAllocator
,
1218 FileAsyncReader_Request
,
1219 FileAsyncReader_WaitForNext
,
1220 FileAsyncReader_SyncReadAligned
,
1221 FileAsyncReader_SyncRead
,
1222 FileAsyncReader_Length
,
1223 FileAsyncReader_BeginFlush
,
1224 FileAsyncReader_EndFlush
,