4 * Copyright 2003 Robert Shearman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
24 #include "quartz_private.h"
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(quartz
);
38 static const WCHAR wszOutputPinName
[] = { 'O','u','t','p','u','t',0 };
40 typedef struct AsyncReader
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 WCHAR
const mediatype_name
[11] = {
67 'M', 'e', 'd', 'i', 'a', ' ', 'T', 'y', 'p', 'e', 0 };
68 static WCHAR
const subtype_name
[8] = {
69 'S', 'u', 'b', 't', 'y', 'p', 'e', 0 };
71 static HRESULT
process_extensions(HKEY hkeyExtensions
, LPCOLESTR pszFileName
, GUID
* majorType
, GUID
* minorType
)
82 /* Get the part of the name that matters */
83 extension
= PathFindExtensionW(pszFileName
);
84 if (*extension
!= '.')
87 l
= RegOpenKeyExW(hkeyExtensions
, extension
, 0, KEY_READ
, &hsub
);
91 size
= sizeof(keying
);
92 l
= RegQueryValueExW(hsub
, mediatype_name
, NULL
, NULL
, (LPBYTE
)keying
, &size
);
94 CLSIDFromString(keying
, majorType
);
96 size
= sizeof(keying
);
98 l
= RegQueryValueExW(hsub
, subtype_name
, NULL
, NULL
, (LPBYTE
)keying
, &size
);
100 CLSIDFromString(keying
, minorType
);
109 static unsigned char byte_from_hex_char(WCHAR wHex
)
111 switch (tolowerW(wHex
))
123 return (wHex
- '0') & 0xf;
130 return (wHex
- 'a' + 10) & 0xf;
136 static HRESULT
process_pattern_string(LPCWSTR wszPatternString
, IAsyncReader
* pReader
)
146 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString
));
148 /* format: "offset, bytestocompare, mask, value" */
150 ulOffset
= strtolW(wszPatternString
, NULL
, 10);
152 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
155 wszPatternString
++; /* skip ',' */
157 ulBytes
= strtolW(wszPatternString
, NULL
, 10);
159 pbMask
= HeapAlloc(GetProcessHeap(), 0, ulBytes
);
160 pbValue
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, ulBytes
);
161 pbFile
= HeapAlloc(GetProcessHeap(), 0, ulBytes
);
163 /* default mask is match everything */
164 memset(pbMask
, 0xFF, ulBytes
);
166 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
169 wszPatternString
++; /* skip ',' */
173 for ( ; !isxdigitW(*wszPatternString
) && (*wszPatternString
!= ','); wszPatternString
++)
176 for (strpos
= 0; isxdigitW(*wszPatternString
) && (strpos
/2 < ulBytes
); wszPatternString
++, strpos
++)
178 if ((strpos
% 2) == 1) /* odd numbered position */
179 pbMask
[strpos
/ 2] |= byte_from_hex_char(*wszPatternString
);
181 pbMask
[strpos
/ 2] = byte_from_hex_char(*wszPatternString
) << 4;
184 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
187 wszPatternString
++; /* skip ',' */
192 for ( ; !isxdigitW(*wszPatternString
) && (*wszPatternString
!= ','); wszPatternString
++)
195 for (strpos
= 0; isxdigitW(*wszPatternString
) && (strpos
/2 < ulBytes
); wszPatternString
++, strpos
++)
197 if ((strpos
% 2) == 1) /* odd numbered position */
198 pbValue
[strpos
/ 2] |= byte_from_hex_char(*wszPatternString
);
200 pbValue
[strpos
/ 2] = byte_from_hex_char(*wszPatternString
) << 4;
205 hr
= IAsyncReader_SyncRead(pReader
, ulOffset
, ulBytes
, pbFile
);
210 for (i
= 0; i
< ulBytes
; i
++)
211 if ((pbFile
[i
] & pbMask
[i
]) != pbValue
[i
])
218 HeapFree(GetProcessHeap(), 0, pbMask
);
219 HeapFree(GetProcessHeap(), 0, pbValue
);
220 HeapFree(GetProcessHeap(), 0, pbFile
);
222 /* if we encountered no errors with this string, and there is a following tuple, then we
223 * have to match that as well to succeed */
224 if ((hr
== S_OK
) && (wszPatternString
= strchrW(wszPatternString
, ',')))
225 return process_pattern_string(wszPatternString
+ 1, pReader
);
230 static HRESULT
GetClassMediaFile(IAsyncReader
* pReader
, LPCOLESTR pszFileName
, GUID
* majorType
, GUID
* minorType
)
232 HKEY hkeyMediaType
= NULL
;
236 static const WCHAR wszMediaType
[] = {'M','e','d','i','a',' ','T','y','p','e',0};
238 TRACE("(%p, %s, %p, %p)\n", pReader
, debugstr_w(pszFileName
), majorType
, minorType
);
240 CopyMemory(majorType
, &GUID_NULL
, sizeof(*majorType
));
241 CopyMemory(minorType
, &GUID_NULL
, sizeof(*minorType
));
243 lRet
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, wszMediaType
, 0, KEY_READ
, &hkeyMediaType
);
244 hr
= HRESULT_FROM_WIN32(lRet
);
250 for (indexMajor
= 0; !bFound
; indexMajor
++)
253 WCHAR wszMajorKeyName
[CHARS_IN_GUID
];
254 DWORD dwKeyNameLength
= sizeof(wszMajorKeyName
) / sizeof(wszMajorKeyName
[0]);
255 static const WCHAR wszExtensions
[] = {'E','x','t','e','n','s','i','o','n','s',0};
257 if (RegEnumKeyExW(hkeyMediaType
, indexMajor
, wszMajorKeyName
, &dwKeyNameLength
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
259 if (RegOpenKeyExW(hkeyMediaType
, wszMajorKeyName
, 0, KEY_READ
, &hkeyMajor
) != ERROR_SUCCESS
)
261 TRACE("%s\n", debugstr_w(wszMajorKeyName
));
262 if (!strcmpW(wszExtensions
, wszMajorKeyName
))
264 if (process_extensions(hkeyMajor
, pszFileName
, majorType
, minorType
) == S_OK
)
271 for (indexMinor
= 0; !bFound
; indexMinor
++)
274 WCHAR wszMinorKeyName
[CHARS_IN_GUID
];
275 DWORD dwMinorKeyNameLen
= sizeof(wszMinorKeyName
) / sizeof(wszMinorKeyName
[0]);
279 if (RegEnumKeyExW(hkeyMajor
, indexMinor
, wszMinorKeyName
, &dwMinorKeyNameLen
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
282 if (RegOpenKeyExW(hkeyMajor
, wszMinorKeyName
, 0, KEY_READ
, &hkeyMinor
) != ERROR_SUCCESS
)
285 TRACE("\t%s\n", debugstr_w(wszMinorKeyName
));
287 if (RegQueryInfoKeyW(hkeyMinor
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &maxValueLen
, NULL
, NULL
) != ERROR_SUCCESS
)
290 for (indexValue
= 0; !bFound
; indexValue
++)
293 WCHAR wszValueName
[14]; /* longest name we should encounter will be "Source Filter" */
294 LPWSTR wszPatternString
= HeapAlloc(GetProcessHeap(), 0, maxValueLen
);
295 DWORD dwValueNameLen
= sizeof(wszValueName
) / sizeof(wszValueName
[0]); /* remember this is in chars */
296 DWORD dwDataLen
= maxValueLen
; /* remember this is in bytes */
297 static const WCHAR wszSourceFilter
[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
300 if ((temp
= RegEnumValueW(hkeyMinor
, indexValue
, wszValueName
, &dwValueNameLen
, NULL
, &dwType
, (LPBYTE
)wszPatternString
, &dwDataLen
)) != ERROR_SUCCESS
)
302 HeapFree(GetProcessHeap(), 0, wszPatternString
);
306 /* if it is not the source filter value */
307 if (strcmpW(wszValueName
, wszSourceFilter
))
309 if (process_pattern_string(wszPatternString
, pReader
) == S_OK
)
311 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName
, majorType
)) &&
312 SUCCEEDED(CLSIDFromString(wszMinorKeyName
, minorType
)))
316 HeapFree(GetProcessHeap(), 0, wszPatternString
);
318 CloseHandle(hkeyMinor
);
321 CloseHandle(hkeyMajor
);
324 CloseHandle(hkeyMediaType
);
326 if (SUCCEEDED(hr
) && !bFound
)
328 ERR("Media class not found\n");
332 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType
), qzdebugstr_guid(minorType
));
337 HRESULT
AsyncReader_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
339 AsyncReader
*pAsyncRead
;
342 return CLASS_E_NOAGGREGATION
;
344 pAsyncRead
= CoTaskMemAlloc(sizeof(AsyncReader
));
347 return E_OUTOFMEMORY
;
349 pAsyncRead
->lpVtbl
= &AsyncReader_Vtbl
;
350 pAsyncRead
->lpVtblFSF
= &FileSource_Vtbl
;
351 pAsyncRead
->refCount
= 1;
352 pAsyncRead
->filterInfo
.achName
[0] = '\0';
353 pAsyncRead
->filterInfo
.pGraph
= NULL
;
354 pAsyncRead
->pOutputPin
= NULL
;
356 InitializeCriticalSection(&pAsyncRead
->csFilter
);
357 pAsyncRead
->csFilter
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": AsyncReader.csFilter");
359 pAsyncRead
->pszFileName
= NULL
;
360 pAsyncRead
->pmt
= NULL
;
362 *ppv
= (LPVOID
)pAsyncRead
;
364 TRACE("-- created at %p\n", pAsyncRead
);
369 /** IUnknown methods **/
371 static HRESULT WINAPI
AsyncReader_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
373 AsyncReader
*This
= (AsyncReader
*)iface
;
375 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
379 if (IsEqualIID(riid
, &IID_IUnknown
))
381 else if (IsEqualIID(riid
, &IID_IPersist
))
383 else if (IsEqualIID(riid
, &IID_IMediaFilter
))
385 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
387 else if (IsEqualIID(riid
, &IID_IFileSourceFilter
))
388 *ppv
= (LPVOID
)(&This
->lpVtblFSF
);
392 IUnknown_AddRef((IUnknown
*)(*ppv
));
396 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
398 return E_NOINTERFACE
;
401 static ULONG WINAPI
AsyncReader_AddRef(IBaseFilter
* iface
)
403 AsyncReader
*This
= (AsyncReader
*)iface
;
404 ULONG refCount
= InterlockedIncrement(&This
->refCount
);
406 TRACE("(%p)->() AddRef from %d\n", This
, refCount
- 1);
411 static ULONG WINAPI
AsyncReader_Release(IBaseFilter
* iface
)
413 AsyncReader
*This
= (AsyncReader
*)iface
;
414 ULONG refCount
= InterlockedDecrement(&This
->refCount
);
416 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
420 if (This
->pOutputPin
)
423 if(SUCCEEDED(IPin_ConnectedTo(This
->pOutputPin
, &pConnectedTo
)))
425 IPin_Disconnect(pConnectedTo
);
426 IPin_Release(pConnectedTo
);
428 IPin_Disconnect(This
->pOutputPin
);
429 IPin_Release(This
->pOutputPin
);
431 This
->csFilter
.DebugInfo
->Spare
[0] = 0;
432 DeleteCriticalSection(&This
->csFilter
);
441 /** IPersist methods **/
443 static HRESULT WINAPI
AsyncReader_GetClassID(IBaseFilter
* iface
, CLSID
* pClsid
)
445 TRACE("(%p)\n", pClsid
);
447 *pClsid
= CLSID_AsyncReader
;
452 /** IMediaFilter methods **/
454 static HRESULT WINAPI
AsyncReader_Stop(IBaseFilter
* iface
)
456 AsyncReader
*This
= (AsyncReader
*)iface
;
460 This
->state
= State_Stopped
;
465 static HRESULT WINAPI
AsyncReader_Pause(IBaseFilter
* iface
)
467 AsyncReader
*This
= (AsyncReader
*)iface
;
471 This
->state
= State_Paused
;
476 static HRESULT WINAPI
AsyncReader_Run(IBaseFilter
* iface
, REFERENCE_TIME tStart
)
478 AsyncReader
*This
= (AsyncReader
*)iface
;
480 TRACE("(%x%08x)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
);
482 This
->state
= State_Running
;
487 static HRESULT WINAPI
AsyncReader_GetState(IBaseFilter
* iface
, DWORD dwMilliSecsTimeout
, FILTER_STATE
*pState
)
489 AsyncReader
*This
= (AsyncReader
*)iface
;
491 TRACE("(%u, %p)\n", dwMilliSecsTimeout
, pState
);
493 *pState
= This
->state
;
498 static HRESULT WINAPI
AsyncReader_SetSyncSource(IBaseFilter
* iface
, IReferenceClock
*pClock
)
500 /* AsyncReader *This = (AsyncReader *)iface;*/
502 TRACE("(%p)\n", pClock
);
507 static HRESULT WINAPI
AsyncReader_GetSyncSource(IBaseFilter
* iface
, IReferenceClock
**ppClock
)
509 /* AsyncReader *This = (AsyncReader *)iface;*/
511 TRACE("(%p)\n", ppClock
);
516 /** IBaseFilter methods **/
518 static HRESULT WINAPI
AsyncReader_EnumPins(IBaseFilter
* iface
, IEnumPins
**ppEnum
)
521 AsyncReader
*This
= (AsyncReader
*)iface
;
523 TRACE("(%p)\n", ppEnum
);
525 epd
.cPins
= This
->pOutputPin
? 1 : 0;
526 epd
.ppPins
= &This
->pOutputPin
;
527 return IEnumPinsImpl_Construct(&epd
, ppEnum
);
530 static HRESULT WINAPI
AsyncReader_FindPin(IBaseFilter
* iface
, LPCWSTR Id
, IPin
**ppPin
)
532 FIXME("(%s, %p)\n", debugstr_w(Id
), ppPin
);
537 static HRESULT WINAPI
AsyncReader_QueryFilterInfo(IBaseFilter
* iface
, FILTER_INFO
*pInfo
)
539 AsyncReader
*This
= (AsyncReader
*)iface
;
541 TRACE("(%p)\n", pInfo
);
543 strcpyW(pInfo
->achName
, This
->filterInfo
.achName
);
544 pInfo
->pGraph
= This
->filterInfo
.pGraph
;
547 IFilterGraph_AddRef(pInfo
->pGraph
);
552 static HRESULT WINAPI
AsyncReader_JoinFilterGraph(IBaseFilter
* iface
, IFilterGraph
*pGraph
, LPCWSTR pName
)
554 AsyncReader
*This
= (AsyncReader
*)iface
;
556 TRACE("(%p, %s)\n", pGraph
, debugstr_w(pName
));
559 strcpyW(This
->filterInfo
.achName
, pName
);
561 *This
->filterInfo
.achName
= 0;
562 This
->filterInfo
.pGraph
= pGraph
; /* NOTE: do NOT increase ref. count */
567 static HRESULT WINAPI
AsyncReader_QueryVendorInfo(IBaseFilter
* iface
, LPWSTR
*pVendorInfo
)
569 FIXME("(%p)\n", pVendorInfo
);
574 static const IBaseFilterVtbl AsyncReader_Vtbl
=
576 AsyncReader_QueryInterface
,
579 AsyncReader_GetClassID
,
583 AsyncReader_GetState
,
584 AsyncReader_SetSyncSource
,
585 AsyncReader_GetSyncSource
,
586 AsyncReader_EnumPins
,
588 AsyncReader_QueryFilterInfo
,
589 AsyncReader_JoinFilterGraph
,
590 AsyncReader_QueryVendorInfo
593 static HRESULT WINAPI
FileSource_QueryInterface(IFileSourceFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
595 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
597 return IBaseFilter_QueryInterface((IFileSourceFilter
*)&This
->lpVtbl
, riid
, ppv
);
600 static ULONG WINAPI
FileSource_AddRef(IFileSourceFilter
* iface
)
602 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
604 return IBaseFilter_AddRef((IFileSourceFilter
*)&This
->lpVtbl
);
607 static ULONG WINAPI
FileSource_Release(IFileSourceFilter
* iface
)
609 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
611 return IBaseFilter_Release((IFileSourceFilter
*)&This
->lpVtbl
);
614 static HRESULT WINAPI
FileSource_Load(IFileSourceFilter
* iface
, LPCOLESTR pszFileName
, const AM_MEDIA_TYPE
* pmt
)
618 IAsyncReader
* pReader
= NULL
;
619 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
621 TRACE("(%s, %p)\n", debugstr_w(pszFileName
), pmt
);
624 /* FIXME: check the sharing values that native uses */
625 hFile
= CreateFileW(pszFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, NULL
);
627 if (hFile
== INVALID_HANDLE_VALUE
)
629 return HRESULT_FROM_WIN32(GetLastError());
633 hr
= FileAsyncReader_Construct(hFile
, (IBaseFilter
*)&This
->lpVtbl
, &This
->csFilter
, &This
->pOutputPin
);
636 hr
= IPin_QueryInterface(This
->pOutputPin
, &IID_IAsyncReader
, (LPVOID
*)&pReader
);
638 /* store file name & media type */
641 This
->pszFileName
= CoTaskMemAlloc((strlenW(pszFileName
) + 1) * sizeof(WCHAR
));
642 strcpyW(This
->pszFileName
, pszFileName
);
643 This
->pmt
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
));
646 This
->pmt
->bFixedSizeSamples
= TRUE
;
647 This
->pmt
->bTemporalCompression
= FALSE
;
648 This
->pmt
->cbFormat
= 0;
649 This
->pmt
->pbFormat
= NULL
;
650 This
->pmt
->pUnk
= NULL
;
651 This
->pmt
->lSampleSize
= 0;
652 This
->pmt
->formattype
= FORMAT_None
;
653 hr
= GetClassMediaFile(pReader
, pszFileName
, &This
->pmt
->majortype
, &This
->pmt
->subtype
);
656 CoTaskMemFree(This
->pmt
);
661 CopyMediaType(This
->pmt
, pmt
);
665 IAsyncReader_Release(pReader
);
669 if (This
->pOutputPin
)
671 IPin_Release(This
->pOutputPin
);
672 This
->pOutputPin
= NULL
;
675 CoTaskMemFree(This
->pszFileName
);
676 This
->pszFileName
= NULL
;
681 /* FIXME: check return codes */
685 static HRESULT WINAPI
FileSource_GetCurFile(IFileSourceFilter
* iface
, LPOLESTR
* ppszFileName
, AM_MEDIA_TYPE
* pmt
)
687 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
689 TRACE("(%p, %p)\n", ppszFileName
, pmt
);
694 /* copy file name & media type if available, otherwise clear the outputs */
695 if (This
->pszFileName
)
697 *ppszFileName
= CoTaskMemAlloc((strlenW(This
->pszFileName
) + 1) * sizeof(WCHAR
));
698 strcpyW(*ppszFileName
, This
->pszFileName
);
701 *ppszFileName
= NULL
;
706 CopyMediaType(pmt
, This
->pmt
);
708 ZeroMemory(pmt
, sizeof(*pmt
));
714 static const IFileSourceFilterVtbl FileSource_Vtbl
=
716 FileSource_QueryInterface
,
720 FileSource_GetCurFile
724 /* the dwUserData passed back to user */
725 typedef struct DATAREQUEST
727 IMediaSample
* pSample
; /* sample passed to us by user */
728 DWORD_PTR dwUserData
; /* user data passed to us */
729 OVERLAPPED ovl
; /* our overlapped structure */
731 struct DATAREQUEST
* pNext
; /* next data request in list */
734 static void queue(DATAREQUEST
* pHead
, DATAREQUEST
* pItem
)
736 DATAREQUEST
* pCurrent
;
737 for (pCurrent
= pHead
; pCurrent
->pNext
; pCurrent
= pCurrent
->pNext
)
739 pCurrent
->pNext
= pItem
;
742 typedef struct FileAsyncReader
745 const struct IAsyncReaderVtbl
* lpVtblAR
;
750 DATAREQUEST
* pHead
; /* head of data request list */
751 CRITICAL_SECTION csList
; /* critical section to protect operations on list */
754 static inline FileAsyncReader
*impl_from_IAsyncReader( IAsyncReader
*iface
)
756 return (FileAsyncReader
*)((char*)iface
- FIELD_OFFSET(FileAsyncReader
, lpVtblAR
));
759 static HRESULT
AcceptProcAFR(LPVOID iface
, const AM_MEDIA_TYPE
*pmt
)
761 AsyncReader
*This
= (AsyncReader
*)iface
;
763 FIXME("(%p, %p)\n", iface
, pmt
);
765 if (IsEqualGUID(&pmt
->majortype
, &This
->pmt
->majortype
) &&
766 IsEqualGUID(&pmt
->subtype
, &This
->pmt
->subtype
) &&
767 IsEqualGUID(&pmt
->formattype
, &FORMAT_None
))
773 /* overriden pin functions */
775 static HRESULT WINAPI
FileAsyncReaderPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
777 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
778 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
782 if (IsEqualIID(riid
, &IID_IUnknown
))
784 else if (IsEqualIID(riid
, &IID_IPin
))
786 else if (IsEqualIID(riid
, &IID_IAsyncReader
))
787 *ppv
= (LPVOID
)&This
->lpVtblAR
;
791 IUnknown_AddRef((IUnknown
*)(*ppv
));
795 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
797 return E_NOINTERFACE
;
800 static ULONG WINAPI
FileAsyncReaderPin_Release(IPin
* iface
)
802 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
803 ULONG refCount
= InterlockedDecrement(&This
->pin
.pin
.refCount
);
805 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
809 DATAREQUEST
* pCurrent
;
811 for (pCurrent
= This
->pHead
; pCurrent
; pCurrent
= pNext
)
813 pNext
= pCurrent
->pNext
;
814 CoTaskMemFree(pCurrent
);
816 CloseHandle(This
->hFile
);
817 CloseHandle(This
->hEvent
);
818 This
->csList
.DebugInfo
->Spare
[0] = 0;
819 DeleteCriticalSection(&This
->csList
);
826 static HRESULT WINAPI
FileAsyncReaderPin_EnumMediaTypes(IPin
* iface
, IEnumMediaTypes
** ppEnum
)
828 ENUMMEDIADETAILS emd
;
829 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
831 TRACE("(%p)\n", ppEnum
);
834 emd
.pMediaTypes
= ((AsyncReader
*)This
->pin
.pin
.pinInfo
.pFilter
)->pmt
;
836 return IEnumMediaTypesImpl_Construct(&emd
, ppEnum
);
839 static const IPinVtbl FileAsyncReaderPin_Vtbl
=
841 FileAsyncReaderPin_QueryInterface
,
843 FileAsyncReaderPin_Release
,
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 /* Function called as a helper to IPin_Connect */
862 /* specific AM_MEDIA_TYPE - it cannot be NULL */
863 /* this differs from standard OutputPin_ConnectSpecific only in that it
864 * doesn't need the IMemInputPin interface on the receiving pin */
865 static HRESULT
FileAsyncReaderPin_ConnectSpecific(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
867 OutputPin
*This
= (OutputPin
*)iface
;
870 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
871 dump_AM_MEDIA_TYPE(pmt
);
873 /* FIXME: call queryacceptproc */
875 This
->pin
.pConnectedTo
= pReceivePin
;
876 IPin_AddRef(pReceivePin
);
877 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
879 hr
= IPin_ReceiveConnection(pReceivePin
, iface
, pmt
);
883 IPin_Release(This
->pin
.pConnectedTo
);
884 This
->pin
.pConnectedTo
= NULL
;
885 FreeMediaType(&This
->pin
.mtCurrent
);
888 TRACE(" -- %x\n", hr
);
892 static HRESULT
FileAsyncReader_Construct(HANDLE hFile
, IBaseFilter
* pBaseFilter
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
894 FileAsyncReader
* pPinImpl
;
899 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
902 return E_OUTOFMEMORY
;
904 piOutput
.dir
= PINDIR_OUTPUT
;
905 piOutput
.pFilter
= pBaseFilter
;
906 strcpyW(piOutput
.achName
, wszOutputPinName
);
908 if (SUCCEEDED(OutputPin_Init(&piOutput
, NULL
, pBaseFilter
, AcceptProcAFR
, pCritSec
, &pPinImpl
->pin
)))
910 pPinImpl
->pin
.pin
.lpVtbl
= &FileAsyncReaderPin_Vtbl
;
911 pPinImpl
->lpVtblAR
= &FileAsyncReader_Vtbl
;
912 pPinImpl
->hFile
= hFile
;
913 pPinImpl
->hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
914 pPinImpl
->bFlushing
= FALSE
;
915 pPinImpl
->pHead
= NULL
;
916 pPinImpl
->pin
.pConnectSpecific
= FileAsyncReaderPin_ConnectSpecific
;
917 InitializeCriticalSection(&pPinImpl
->csList
);
918 pPinImpl
->csList
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FileAsyncReader.csList");
920 *ppPin
= (IPin
*)(&pPinImpl
->pin
.pin
.lpVtbl
);
924 CoTaskMemFree(pPinImpl
);
930 static HRESULT WINAPI
FileAsyncReader_QueryInterface(IAsyncReader
* iface
, REFIID riid
, LPVOID
* ppv
)
932 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
934 return IPin_QueryInterface((IPin
*)This
, riid
, ppv
);
937 static ULONG WINAPI
FileAsyncReader_AddRef(IAsyncReader
* iface
)
939 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
941 return IPin_AddRef((IPin
*)This
);
944 static ULONG WINAPI
FileAsyncReader_Release(IAsyncReader
* iface
)
946 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
948 return IPin_Release((IPin
*)This
);
951 #define DEF_ALIGNMENT 1
953 static HRESULT WINAPI
FileAsyncReader_RequestAllocator(IAsyncReader
* iface
, IMemAllocator
* pPreferred
, ALLOCATOR_PROPERTIES
* pProps
, IMemAllocator
** ppActual
)
957 TRACE("(%p, %p, %p)\n", pPreferred
, pProps
, ppActual
);
959 if (!pProps
->cbAlign
|| (pProps
->cbAlign
% DEF_ALIGNMENT
) != 0)
960 pProps
->cbAlign
= DEF_ALIGNMENT
;
964 ALLOCATOR_PROPERTIES PropsActual
;
965 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, &PropsActual
);
966 /* FIXME: check we are still aligned */
969 IMemAllocator_AddRef(pPreferred
);
970 *ppActual
= pPreferred
;
971 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr
);
978 hr
= CoCreateInstance(&CLSID_MemoryAllocator
, NULL
, CLSCTX_INPROC
, &IID_IMemAllocator
, (LPVOID
*)&pPreferred
);
982 ALLOCATOR_PROPERTIES PropsActual
;
983 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, &PropsActual
);
984 /* FIXME: check we are still aligned */
987 *ppActual
= pPreferred
;
988 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr
);
997 IMemAllocator_Release(pPreferred
);
1000 TRACE("-- %x\n", hr
);
1004 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
1005 * however, this would be quite complicated to do and may be a bit error prone */
1006 static HRESULT WINAPI
FileAsyncReader_Request(IAsyncReader
* iface
, IMediaSample
* pSample
, DWORD_PTR dwUser
)
1008 REFERENCE_TIME Start
;
1009 REFERENCE_TIME Stop
;
1010 DATAREQUEST
* pDataRq
;
1013 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1015 TRACE("(%p, %lx)\n", pSample
, dwUser
);
1017 /* check flushing state */
1018 if (This
->bFlushing
)
1019 return VFW_E_WRONG_STATE
;
1021 if (!(pDataRq
= CoTaskMemAlloc(sizeof(*pDataRq
))))
1024 /* get start and stop positions in bytes */
1026 hr
= IMediaSample_GetTime(pSample
, &Start
, &Stop
);
1029 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
1033 DWORD dwLength
= (DWORD
) BYTES_FROM_MEDIATIME(Stop
- Start
);
1035 pDataRq
->ovl
.u
.s
.Offset
= (DWORD
) BYTES_FROM_MEDIATIME(Start
);
1036 pDataRq
->ovl
.u
.s
.OffsetHigh
= (DWORD
)(BYTES_FROM_MEDIATIME(Start
) >> (sizeof(DWORD
) * 8));
1037 pDataRq
->ovl
.hEvent
= This
->hEvent
;
1038 pDataRq
->dwUserData
= dwUser
;
1039 pDataRq
->pNext
= NULL
;
1040 /* we violate traditional COM rules here by maintaining
1041 * a reference to the sample, but not calling AddRef, but
1042 * that's what MSDN says to do */
1043 pDataRq
->pSample
= pSample
;
1045 EnterCriticalSection(&This
->csList
);
1048 /* adds data request to end of list */
1049 queue(This
->pHead
, pDataRq
);
1051 This
->pHead
= pDataRq
;
1053 LeaveCriticalSection(&This
->csList
);
1055 /* this is definitely not how it is implemented on Win9x
1056 * as they do not support async reads on files, but it is
1057 * sooo much easier to use this than messing around with threads!
1059 if (!ReadFile(This
->hFile
, pBuffer
, dwLength
, NULL
, &pDataRq
->ovl
))
1060 hr
= HRESULT_FROM_WIN32(GetLastError());
1062 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1063 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1067 if (FAILED(hr
) && pDataRq
)
1069 EnterCriticalSection(&This
->csList
);
1071 DATAREQUEST
* pCurrent
;
1072 for (pCurrent
= This
->pHead
; pCurrent
&& pCurrent
->pNext
; pCurrent
= pCurrent
->pNext
)
1073 if (pCurrent
->pNext
== pDataRq
)
1075 pCurrent
->pNext
= pDataRq
->pNext
;
1079 LeaveCriticalSection(&This
->csList
);
1080 CoTaskMemFree(pDataRq
);
1083 TRACE("-- %x\n", hr
);
1087 static HRESULT WINAPI
FileAsyncReader_WaitForNext(IAsyncReader
* iface
, DWORD dwTimeout
, IMediaSample
** ppSample
, DWORD_PTR
* pdwUser
)
1091 DATAREQUEST
* pDataRq
= NULL
;
1092 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1094 TRACE("(%u, %p, %p)\n", dwTimeout
, ppSample
, pdwUser
);
1096 /* FIXME: we could do with improving this by waiting for an array of event handles
1097 * and then determining which one finished and removing that from the list, otherwise
1098 * we will end up waiting for longer than we should do, if a later request finishes
1099 * before an earlier one */
1104 /* we return immediately if flushing */
1105 if (This
->bFlushing
)
1106 hr
= VFW_E_WRONG_STATE
;
1110 /* wait for the read to finish or timeout */
1111 if (WaitForSingleObject(This
->hEvent
, dwTimeout
) == WAIT_TIMEOUT
)
1116 EnterCriticalSection(&This
->csList
);
1118 pDataRq
= This
->pHead
;
1119 if (pDataRq
== NULL
)
1122 This
->pHead
= pDataRq
->pNext
;
1124 LeaveCriticalSection(&This
->csList
);
1129 /* get any errors */
1130 if (!GetOverlappedResult(This
->hFile
, &pDataRq
->ovl
, &dwBytes
, FALSE
))
1131 hr
= HRESULT_FROM_WIN32(GetLastError());
1136 IMediaSample_SetActualDataLength(pDataRq
->pSample
, dwBytes
);
1137 *ppSample
= pDataRq
->pSample
;
1138 *pdwUser
= pDataRq
->dwUserData
;
1141 /* no need to close event handle since we will close it when the pin is destroyed */
1142 CoTaskMemFree(pDataRq
);
1144 TRACE("-- %x\n", hr
);
1148 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
);
1150 static HRESULT WINAPI
FileAsyncReader_SyncReadAligned(IAsyncReader
* iface
, IMediaSample
* pSample
)
1153 REFERENCE_TIME tStart
;
1154 REFERENCE_TIME tStop
;
1157 TRACE("(%p)\n", pSample
);
1159 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
1162 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
1165 hr
= FileAsyncReader_SyncRead(iface
,
1166 BYTES_FROM_MEDIATIME(tStart
),
1167 (LONG
) BYTES_FROM_MEDIATIME(tStop
- tStart
),
1170 TRACE("-- %x\n", hr
);
1174 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
)
1178 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1180 TRACE("(%x%08x, %d, %p)\n", (ULONG
)(llPosition
>> 32), (ULONG
)llPosition
, lLength
, pBuffer
);
1182 ZeroMemory(&ovl
, sizeof(ovl
));
1184 ovl
.hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
1185 /* NOTE: llPosition is the actual byte position to start reading from */
1186 ovl
.u
.s
.Offset
= (DWORD
) llPosition
;
1187 ovl
.u
.s
.OffsetHigh
= (DWORD
) (llPosition
>> (sizeof(DWORD
) * 8));
1189 if (!ReadFile(This
->hFile
, pBuffer
, lLength
, NULL
, &ovl
))
1190 hr
= HRESULT_FROM_WIN32(GetLastError());
1192 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1199 if (!GetOverlappedResult(This
->hFile
, &ovl
, &dwBytesRead
, TRUE
))
1200 hr
= HRESULT_FROM_WIN32(GetLastError());
1203 CloseHandle(ovl
.hEvent
);
1205 TRACE("-- %x\n", hr
);
1209 static HRESULT WINAPI
FileAsyncReader_Length(IAsyncReader
* iface
, LONGLONG
* pTotal
, LONGLONG
* pAvailable
)
1213 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1215 TRACE("(%p, %p)\n", pTotal
, pAvailable
);
1217 if (((dwSizeLow
= GetFileSize(This
->hFile
, &dwSizeHigh
)) == -1) &&
1218 (GetLastError() != NO_ERROR
))
1219 return HRESULT_FROM_WIN32(GetLastError());
1221 *pTotal
= (LONGLONG
)dwSizeLow
| (LONGLONG
)dwSizeHigh
<< (sizeof(DWORD
) * 8);
1223 *pAvailable
= *pTotal
;
1228 static HRESULT WINAPI
FileAsyncReader_BeginFlush(IAsyncReader
* iface
)
1230 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1234 This
->bFlushing
= TRUE
;
1235 CancelIo(This
->hFile
);
1236 SetEvent(This
->hEvent
);
1238 /* FIXME: free list */
1243 static HRESULT WINAPI
FileAsyncReader_EndFlush(IAsyncReader
* iface
)
1245 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1249 This
->bFlushing
= FALSE
;
1254 static const IAsyncReaderVtbl FileAsyncReader_Vtbl
=
1256 FileAsyncReader_QueryInterface
,
1257 FileAsyncReader_AddRef
,
1258 FileAsyncReader_Release
,
1259 FileAsyncReader_RequestAllocator
,
1260 FileAsyncReader_Request
,
1261 FileAsyncReader_WaitForNext
,
1262 FileAsyncReader_SyncReadAligned
,
1263 FileAsyncReader_SyncRead
,
1264 FileAsyncReader_Length
,
1265 FileAsyncReader_BeginFlush
,
1266 FileAsyncReader_EndFlush
,