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
);
101 CLSIDFromString(keying
, minorType
);
110 static unsigned char byte_from_hex_char(WCHAR wHex
)
112 switch (tolowerW(wHex
))
124 return (wHex
- '0') & 0xf;
131 return (wHex
- 'a' + 10) & 0xf;
137 static HRESULT
process_pattern_string(LPCWSTR wszPatternString
, IAsyncReader
* pReader
)
147 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString
));
149 /* format: "offset, bytestocompare, mask, value" */
151 ulOffset
= strtolW(wszPatternString
, NULL
, 10);
153 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
156 wszPatternString
++; /* skip ',' */
158 ulBytes
= strtolW(wszPatternString
, NULL
, 10);
160 pbMask
= HeapAlloc(GetProcessHeap(), 0, ulBytes
);
161 pbValue
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, ulBytes
);
162 pbFile
= HeapAlloc(GetProcessHeap(), 0, ulBytes
);
164 /* default mask is match everything */
165 memset(pbMask
, 0xFF, ulBytes
);
167 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
170 wszPatternString
++; /* skip ',' */
174 for ( ; !isxdigitW(*wszPatternString
) && (*wszPatternString
!= ','); wszPatternString
++)
177 for (strpos
= 0; isxdigitW(*wszPatternString
) && (strpos
/2 < ulBytes
); wszPatternString
++, strpos
++)
179 if ((strpos
% 2) == 1) /* odd numbered position */
180 pbMask
[strpos
/ 2] |= byte_from_hex_char(*wszPatternString
);
182 pbMask
[strpos
/ 2] = byte_from_hex_char(*wszPatternString
) << 4;
185 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
188 wszPatternString
++; /* skip ',' */
193 for ( ; !isxdigitW(*wszPatternString
) && (*wszPatternString
!= ','); wszPatternString
++)
196 for (strpos
= 0; isxdigitW(*wszPatternString
) && (strpos
/2 < ulBytes
); wszPatternString
++, strpos
++)
198 if ((strpos
% 2) == 1) /* odd numbered position */
199 pbValue
[strpos
/ 2] |= byte_from_hex_char(*wszPatternString
);
201 pbValue
[strpos
/ 2] = byte_from_hex_char(*wszPatternString
) << 4;
206 hr
= IAsyncReader_SyncRead(pReader
, ulOffset
, ulBytes
, pbFile
);
211 for (i
= 0; i
< ulBytes
; i
++)
212 if ((pbFile
[i
] & pbMask
[i
]) != pbValue
[i
])
219 HeapFree(GetProcessHeap(), 0, pbMask
);
220 HeapFree(GetProcessHeap(), 0, pbValue
);
221 HeapFree(GetProcessHeap(), 0, pbFile
);
223 /* if we encountered no errors with this string, and there is a following tuple, then we
224 * have to match that as well to succeed */
225 if ((hr
== S_OK
) && (wszPatternString
= strchrW(wszPatternString
, ',')))
226 return process_pattern_string(wszPatternString
+ 1, pReader
);
231 static HRESULT
GetClassMediaFile(IAsyncReader
* pReader
, LPCOLESTR pszFileName
, GUID
* majorType
, GUID
* minorType
)
233 HKEY hkeyMediaType
= NULL
;
237 static const WCHAR wszMediaType
[] = {'M','e','d','i','a',' ','T','y','p','e',0};
239 TRACE("(%p, %s, %p, %p)\n", pReader
, debugstr_w(pszFileName
), majorType
, minorType
);
241 *majorType
= GUID_NULL
;
242 *minorType
= GUID_NULL
;
244 lRet
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, wszMediaType
, 0, KEY_READ
, &hkeyMediaType
);
245 hr
= HRESULT_FROM_WIN32(lRet
);
251 for (indexMajor
= 0; !bFound
; indexMajor
++)
254 WCHAR wszMajorKeyName
[CHARS_IN_GUID
];
255 DWORD dwKeyNameLength
= sizeof(wszMajorKeyName
) / sizeof(wszMajorKeyName
[0]);
256 static const WCHAR wszExtensions
[] = {'E','x','t','e','n','s','i','o','n','s',0};
258 if (RegEnumKeyExW(hkeyMediaType
, indexMajor
, wszMajorKeyName
, &dwKeyNameLength
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
260 if (RegOpenKeyExW(hkeyMediaType
, wszMajorKeyName
, 0, KEY_READ
, &hkeyMajor
) != ERROR_SUCCESS
)
262 TRACE("%s\n", debugstr_w(wszMajorKeyName
));
263 if (!strcmpW(wszExtensions
, wszMajorKeyName
))
265 if (process_extensions(hkeyMajor
, pszFileName
, majorType
, minorType
) == S_OK
)
272 for (indexMinor
= 0; !bFound
; indexMinor
++)
275 WCHAR wszMinorKeyName
[CHARS_IN_GUID
];
276 DWORD dwMinorKeyNameLen
= sizeof(wszMinorKeyName
) / sizeof(wszMinorKeyName
[0]);
280 if (RegEnumKeyExW(hkeyMajor
, indexMinor
, wszMinorKeyName
, &dwMinorKeyNameLen
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
283 if (RegOpenKeyExW(hkeyMajor
, wszMinorKeyName
, 0, KEY_READ
, &hkeyMinor
) != ERROR_SUCCESS
)
286 TRACE("\t%s\n", debugstr_w(wszMinorKeyName
));
288 if (RegQueryInfoKeyW(hkeyMinor
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &maxValueLen
, NULL
, NULL
) != ERROR_SUCCESS
)
291 for (indexValue
= 0; !bFound
; indexValue
++)
294 WCHAR wszValueName
[14]; /* longest name we should encounter will be "Source Filter" */
295 LPWSTR wszPatternString
= HeapAlloc(GetProcessHeap(), 0, maxValueLen
);
296 DWORD dwValueNameLen
= sizeof(wszValueName
) / sizeof(wszValueName
[0]); /* remember this is in chars */
297 DWORD dwDataLen
= maxValueLen
; /* remember this is in bytes */
298 static const WCHAR wszSourceFilter
[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
301 if ((temp
= RegEnumValueW(hkeyMinor
, indexValue
, wszValueName
, &dwValueNameLen
, NULL
, &dwType
, (LPBYTE
)wszPatternString
, &dwDataLen
)) != ERROR_SUCCESS
)
303 HeapFree(GetProcessHeap(), 0, wszPatternString
);
307 /* if it is not the source filter value */
308 if (strcmpW(wszValueName
, wszSourceFilter
))
310 if (process_pattern_string(wszPatternString
, pReader
) == S_OK
)
312 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName
, majorType
)) &&
313 SUCCEEDED(CLSIDFromString(wszMinorKeyName
, minorType
)))
317 HeapFree(GetProcessHeap(), 0, wszPatternString
);
319 CloseHandle(hkeyMinor
);
322 CloseHandle(hkeyMajor
);
325 CloseHandle(hkeyMediaType
);
327 if (SUCCEEDED(hr
) && !bFound
)
329 ERR("Media class not found\n");
333 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType
), qzdebugstr_guid(minorType
));
338 HRESULT
AsyncReader_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
340 AsyncReader
*pAsyncRead
;
343 return CLASS_E_NOAGGREGATION
;
345 pAsyncRead
= CoTaskMemAlloc(sizeof(AsyncReader
));
348 return E_OUTOFMEMORY
;
350 pAsyncRead
->lpVtbl
= &AsyncReader_Vtbl
;
351 pAsyncRead
->lpVtblFSF
= &FileSource_Vtbl
;
352 pAsyncRead
->refCount
= 1;
353 pAsyncRead
->filterInfo
.achName
[0] = '\0';
354 pAsyncRead
->filterInfo
.pGraph
= NULL
;
355 pAsyncRead
->pOutputPin
= NULL
;
357 InitializeCriticalSection(&pAsyncRead
->csFilter
);
358 pAsyncRead
->csFilter
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": AsyncReader.csFilter");
360 pAsyncRead
->pszFileName
= NULL
;
361 pAsyncRead
->pmt
= NULL
;
363 *ppv
= (LPVOID
)pAsyncRead
;
365 TRACE("-- created at %p\n", pAsyncRead
);
370 /** IUnknown methods **/
372 static HRESULT WINAPI
AsyncReader_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
374 AsyncReader
*This
= (AsyncReader
*)iface
;
376 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
380 if (IsEqualIID(riid
, &IID_IUnknown
))
382 else if (IsEqualIID(riid
, &IID_IPersist
))
384 else if (IsEqualIID(riid
, &IID_IMediaFilter
))
386 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
388 else if (IsEqualIID(riid
, &IID_IFileSourceFilter
))
389 *ppv
= (LPVOID
)(&This
->lpVtblFSF
);
393 IUnknown_AddRef((IUnknown
*)(*ppv
));
397 if (!IsEqualIID(riid
, &IID_IPin
) && !IsEqualIID(riid
, &IID_IMediaSeeking
) && !IsEqualIID(riid
, &IID_IVideoWindow
))
398 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
400 return E_NOINTERFACE
;
403 static ULONG WINAPI
AsyncReader_AddRef(IBaseFilter
* iface
)
405 AsyncReader
*This
= (AsyncReader
*)iface
;
406 ULONG refCount
= InterlockedIncrement(&This
->refCount
);
408 TRACE("(%p)->() AddRef from %d\n", This
, refCount
- 1);
413 static ULONG WINAPI
AsyncReader_Release(IBaseFilter
* iface
)
415 AsyncReader
*This
= (AsyncReader
*)iface
;
416 ULONG refCount
= InterlockedDecrement(&This
->refCount
);
418 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
422 if (This
->pOutputPin
)
425 if(SUCCEEDED(IPin_ConnectedTo(This
->pOutputPin
, &pConnectedTo
)))
427 IPin_Disconnect(pConnectedTo
);
428 IPin_Release(pConnectedTo
);
430 IPin_Disconnect(This
->pOutputPin
);
431 IPin_Release(This
->pOutputPin
);
433 This
->csFilter
.DebugInfo
->Spare
[0] = 0;
434 DeleteCriticalSection(&This
->csFilter
);
436 CoTaskMemFree(This
->pszFileName
);
438 FreeMediaType(This
->pmt
);
446 /** IPersist methods **/
448 static HRESULT WINAPI
AsyncReader_GetClassID(IBaseFilter
* iface
, CLSID
* pClsid
)
450 TRACE("(%p)\n", pClsid
);
452 *pClsid
= CLSID_AsyncReader
;
457 /** IMediaFilter methods **/
459 static HRESULT WINAPI
AsyncReader_Stop(IBaseFilter
* iface
)
461 AsyncReader
*This
= (AsyncReader
*)iface
;
465 This
->state
= State_Stopped
;
470 static HRESULT WINAPI
AsyncReader_Pause(IBaseFilter
* iface
)
472 AsyncReader
*This
= (AsyncReader
*)iface
;
476 This
->state
= State_Paused
;
481 static HRESULT WINAPI
AsyncReader_Run(IBaseFilter
* iface
, REFERENCE_TIME tStart
)
483 AsyncReader
*This
= (AsyncReader
*)iface
;
485 TRACE("(%x%08x)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
);
487 This
->state
= State_Running
;
492 static HRESULT WINAPI
AsyncReader_GetState(IBaseFilter
* iface
, DWORD dwMilliSecsTimeout
, FILTER_STATE
*pState
)
494 AsyncReader
*This
= (AsyncReader
*)iface
;
496 TRACE("(%u, %p)\n", dwMilliSecsTimeout
, pState
);
498 *pState
= This
->state
;
503 static HRESULT WINAPI
AsyncReader_SetSyncSource(IBaseFilter
* iface
, IReferenceClock
*pClock
)
505 /* AsyncReader *This = (AsyncReader *)iface;*/
507 TRACE("(%p)\n", pClock
);
512 static HRESULT WINAPI
AsyncReader_GetSyncSource(IBaseFilter
* iface
, IReferenceClock
**ppClock
)
514 /* AsyncReader *This = (AsyncReader *)iface;*/
516 TRACE("(%p)\n", ppClock
);
521 /** IBaseFilter methods **/
523 static HRESULT WINAPI
AsyncReader_EnumPins(IBaseFilter
* iface
, IEnumPins
**ppEnum
)
526 AsyncReader
*This
= (AsyncReader
*)iface
;
528 TRACE("(%p)\n", ppEnum
);
530 epd
.cPins
= This
->pOutputPin
? 1 : 0;
531 epd
.ppPins
= &This
->pOutputPin
;
532 return IEnumPinsImpl_Construct(&epd
, ppEnum
);
535 static HRESULT WINAPI
AsyncReader_FindPin(IBaseFilter
* iface
, LPCWSTR Id
, IPin
**ppPin
)
537 FIXME("(%s, %p)\n", debugstr_w(Id
), ppPin
);
542 static HRESULT WINAPI
AsyncReader_QueryFilterInfo(IBaseFilter
* iface
, FILTER_INFO
*pInfo
)
544 AsyncReader
*This
= (AsyncReader
*)iface
;
546 TRACE("(%p)\n", pInfo
);
548 strcpyW(pInfo
->achName
, This
->filterInfo
.achName
);
549 pInfo
->pGraph
= This
->filterInfo
.pGraph
;
552 IFilterGraph_AddRef(pInfo
->pGraph
);
557 static HRESULT WINAPI
AsyncReader_JoinFilterGraph(IBaseFilter
* iface
, IFilterGraph
*pGraph
, LPCWSTR pName
)
559 AsyncReader
*This
= (AsyncReader
*)iface
;
561 TRACE("(%p, %s)\n", pGraph
, debugstr_w(pName
));
564 strcpyW(This
->filterInfo
.achName
, pName
);
566 *This
->filterInfo
.achName
= 0;
567 This
->filterInfo
.pGraph
= pGraph
; /* NOTE: do NOT increase ref. count */
572 static HRESULT WINAPI
AsyncReader_QueryVendorInfo(IBaseFilter
* iface
, LPWSTR
*pVendorInfo
)
574 FIXME("(%p)\n", pVendorInfo
);
579 static const IBaseFilterVtbl AsyncReader_Vtbl
=
581 AsyncReader_QueryInterface
,
584 AsyncReader_GetClassID
,
588 AsyncReader_GetState
,
589 AsyncReader_SetSyncSource
,
590 AsyncReader_GetSyncSource
,
591 AsyncReader_EnumPins
,
593 AsyncReader_QueryFilterInfo
,
594 AsyncReader_JoinFilterGraph
,
595 AsyncReader_QueryVendorInfo
598 static HRESULT WINAPI
FileSource_QueryInterface(IFileSourceFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
600 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
602 return IBaseFilter_QueryInterface((IFileSourceFilter
*)&This
->lpVtbl
, riid
, ppv
);
605 static ULONG WINAPI
FileSource_AddRef(IFileSourceFilter
* iface
)
607 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
609 return IBaseFilter_AddRef((IFileSourceFilter
*)&This
->lpVtbl
);
612 static ULONG WINAPI
FileSource_Release(IFileSourceFilter
* iface
)
614 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
616 return IBaseFilter_Release((IFileSourceFilter
*)&This
->lpVtbl
);
619 static HRESULT WINAPI
FileSource_Load(IFileSourceFilter
* iface
, LPCOLESTR pszFileName
, const AM_MEDIA_TYPE
* pmt
)
623 IAsyncReader
* pReader
= NULL
;
624 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
626 TRACE("(%s, %p)\n", debugstr_w(pszFileName
), pmt
);
629 /* FIXME: check the sharing values that native uses */
630 hFile
= CreateFileW(pszFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, NULL
);
632 if (hFile
== INVALID_HANDLE_VALUE
)
634 return HRESULT_FROM_WIN32(GetLastError());
638 hr
= FileAsyncReader_Construct(hFile
, (IBaseFilter
*)&This
->lpVtbl
, &This
->csFilter
, &This
->pOutputPin
);
641 hr
= IPin_QueryInterface(This
->pOutputPin
, &IID_IAsyncReader
, (LPVOID
*)&pReader
);
643 /* store file name & media type */
646 CoTaskMemFree(This
->pszFileName
);
648 FreeMediaType(This
->pmt
);
650 This
->pszFileName
= CoTaskMemAlloc((strlenW(pszFileName
) + 1) * sizeof(WCHAR
));
651 strcpyW(This
->pszFileName
, pszFileName
);
653 This
->pmt
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
));
656 This
->pmt
->bFixedSizeSamples
= TRUE
;
657 This
->pmt
->bTemporalCompression
= FALSE
;
658 This
->pmt
->cbFormat
= 0;
659 This
->pmt
->pbFormat
= NULL
;
660 This
->pmt
->pUnk
= NULL
;
661 This
->pmt
->lSampleSize
= 0;
662 This
->pmt
->formattype
= FORMAT_None
;
663 hr
= GetClassMediaFile(pReader
, pszFileName
, &This
->pmt
->majortype
, &This
->pmt
->subtype
);
666 CoTaskMemFree(This
->pmt
);
671 CopyMediaType(This
->pmt
, pmt
);
675 IAsyncReader_Release(pReader
);
679 if (This
->pOutputPin
)
681 IPin_Release(This
->pOutputPin
);
682 This
->pOutputPin
= NULL
;
685 CoTaskMemFree(This
->pszFileName
);
687 FreeMediaType(This
->pmt
);
688 This
->pszFileName
= NULL
;
694 /* FIXME: check return codes */
698 static HRESULT WINAPI
FileSource_GetCurFile(IFileSourceFilter
* iface
, LPOLESTR
* ppszFileName
, AM_MEDIA_TYPE
* pmt
)
700 AsyncReader
*This
= impl_from_IFileSourceFilter(iface
);
702 TRACE("(%p, %p)\n", ppszFileName
, pmt
);
707 /* copy file name & media type if available, otherwise clear the outputs */
708 if (This
->pszFileName
)
710 *ppszFileName
= CoTaskMemAlloc((strlenW(This
->pszFileName
) + 1) * sizeof(WCHAR
));
711 strcpyW(*ppszFileName
, This
->pszFileName
);
714 *ppszFileName
= NULL
;
719 CopyMediaType(pmt
, This
->pmt
);
721 ZeroMemory(pmt
, sizeof(*pmt
));
727 static const IFileSourceFilterVtbl FileSource_Vtbl
=
729 FileSource_QueryInterface
,
733 FileSource_GetCurFile
737 /* the dwUserData passed back to user */
738 typedef struct DATAREQUEST
740 IMediaSample
* pSample
; /* sample passed to us by user */
741 DWORD_PTR dwUserData
; /* user data passed to us */
742 OVERLAPPED ovl
; /* our overlapped structure */
744 struct DATAREQUEST
* pNext
; /* next data request in list */
747 static void queue(DATAREQUEST
* pHead
, DATAREQUEST
* pItem
)
749 DATAREQUEST
* pCurrent
;
750 for (pCurrent
= pHead
; pCurrent
->pNext
; pCurrent
= pCurrent
->pNext
)
752 pCurrent
->pNext
= pItem
;
755 typedef struct FileAsyncReader
758 const struct IAsyncReaderVtbl
* lpVtblAR
;
763 DATAREQUEST
* pHead
; /* head of data request list */
764 CRITICAL_SECTION csList
; /* critical section to protect operations on list */
767 static inline FileAsyncReader
*impl_from_IAsyncReader( IAsyncReader
*iface
)
769 return (FileAsyncReader
*)((char*)iface
- FIELD_OFFSET(FileAsyncReader
, lpVtblAR
));
772 static HRESULT
AcceptProcAFR(LPVOID iface
, const AM_MEDIA_TYPE
*pmt
)
774 AsyncReader
*This
= (AsyncReader
*)iface
;
776 FIXME("(%p, %p)\n", iface
, pmt
);
778 if (IsEqualGUID(&pmt
->majortype
, &This
->pmt
->majortype
) &&
779 IsEqualGUID(&pmt
->subtype
, &This
->pmt
->subtype
) &&
780 IsEqualGUID(&pmt
->formattype
, &FORMAT_None
))
786 /* overriden pin functions */
788 static HRESULT WINAPI
FileAsyncReaderPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
790 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
791 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
795 if (IsEqualIID(riid
, &IID_IUnknown
))
797 else if (IsEqualIID(riid
, &IID_IPin
))
799 else if (IsEqualIID(riid
, &IID_IAsyncReader
))
800 *ppv
= (LPVOID
)&This
->lpVtblAR
;
804 IUnknown_AddRef((IUnknown
*)(*ppv
));
808 if (!IsEqualIID(riid
, &IID_IPin
) && !IsEqualIID(riid
, &IID_IMediaSeeking
))
809 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
811 return E_NOINTERFACE
;
814 static ULONG WINAPI
FileAsyncReaderPin_Release(IPin
* iface
)
816 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
817 ULONG refCount
= InterlockedDecrement(&This
->pin
.pin
.refCount
);
819 TRACE("(%p)->() Release from %d\n", This
, refCount
+ 1);
823 DATAREQUEST
* pCurrent
;
825 for (pCurrent
= This
->pHead
; pCurrent
; pCurrent
= pNext
)
827 pNext
= pCurrent
->pNext
;
828 CoTaskMemFree(pCurrent
);
830 CloseHandle(This
->hFile
);
831 CloseHandle(This
->hEvent
);
832 This
->csList
.DebugInfo
->Spare
[0] = 0;
833 DeleteCriticalSection(&This
->csList
);
840 static HRESULT WINAPI
FileAsyncReaderPin_EnumMediaTypes(IPin
* iface
, IEnumMediaTypes
** ppEnum
)
842 ENUMMEDIADETAILS emd
;
843 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
845 TRACE("(%p)\n", ppEnum
);
848 emd
.pMediaTypes
= ((AsyncReader
*)This
->pin
.pin
.pinInfo
.pFilter
)->pmt
;
850 return IEnumMediaTypesImpl_Construct(&emd
, ppEnum
);
853 static const IPinVtbl FileAsyncReaderPin_Vtbl
=
855 FileAsyncReaderPin_QueryInterface
,
857 FileAsyncReaderPin_Release
,
859 OutputPin_ReceiveConnection
,
861 IPinImpl_ConnectedTo
,
862 IPinImpl_ConnectionMediaType
,
863 IPinImpl_QueryPinInfo
,
864 IPinImpl_QueryDirection
,
866 IPinImpl_QueryAccept
,
867 FileAsyncReaderPin_EnumMediaTypes
,
868 IPinImpl_QueryInternalConnections
,
869 OutputPin_EndOfStream
,
870 OutputPin_BeginFlush
,
875 /* Function called as a helper to IPin_Connect */
876 /* specific AM_MEDIA_TYPE - it cannot be NULL */
877 /* this differs from standard OutputPin_ConnectSpecific only in that it
878 * doesn't need the IMemInputPin interface on the receiving pin */
879 static HRESULT
FileAsyncReaderPin_ConnectSpecific(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
881 OutputPin
*This
= (OutputPin
*)iface
;
884 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
885 dump_AM_MEDIA_TYPE(pmt
);
887 /* FIXME: call queryacceptproc */
889 This
->pin
.pConnectedTo
= pReceivePin
;
890 IPin_AddRef(pReceivePin
);
891 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
893 hr
= IPin_ReceiveConnection(pReceivePin
, iface
, pmt
);
897 IPin_Release(This
->pin
.pConnectedTo
);
898 This
->pin
.pConnectedTo
= NULL
;
899 FreeMediaType(&This
->pin
.mtCurrent
);
902 TRACE(" -- %x\n", hr
);
906 static HRESULT
FileAsyncReader_Construct(HANDLE hFile
, IBaseFilter
* pBaseFilter
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
912 piOutput
.dir
= PINDIR_OUTPUT
;
913 piOutput
.pFilter
= pBaseFilter
;
914 strcpyW(piOutput
.achName
, wszOutputPinName
);
915 hr
= OutputPin_Construct(&FileAsyncReaderPin_Vtbl
, sizeof(FileAsyncReader
), &piOutput
, NULL
, pBaseFilter
, AcceptProcAFR
, pCritSec
, ppPin
);
919 FileAsyncReader
*pPinImpl
= (FileAsyncReader
*)*ppPin
;
920 pPinImpl
->lpVtblAR
= &FileAsyncReader_Vtbl
;
921 pPinImpl
->hFile
= hFile
;
922 pPinImpl
->hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
923 pPinImpl
->bFlushing
= FALSE
;
924 pPinImpl
->pHead
= NULL
;
925 pPinImpl
->pin
.pConnectSpecific
= FileAsyncReaderPin_ConnectSpecific
;
926 InitializeCriticalSection(&pPinImpl
->csList
);
927 pPinImpl
->csList
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": FileAsyncReader.csList");
934 static HRESULT WINAPI
FileAsyncReader_QueryInterface(IAsyncReader
* iface
, REFIID riid
, LPVOID
* ppv
)
936 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
938 return IPin_QueryInterface((IPin
*)This
, riid
, ppv
);
941 static ULONG WINAPI
FileAsyncReader_AddRef(IAsyncReader
* iface
)
943 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
945 return IPin_AddRef((IPin
*)This
);
948 static ULONG WINAPI
FileAsyncReader_Release(IAsyncReader
* iface
)
950 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
952 return IPin_Release((IPin
*)This
);
955 #define DEF_ALIGNMENT 1
957 static HRESULT WINAPI
FileAsyncReader_RequestAllocator(IAsyncReader
* iface
, IMemAllocator
* pPreferred
, ALLOCATOR_PROPERTIES
* pProps
, IMemAllocator
** ppActual
)
961 TRACE("(%p, %p, %p)\n", pPreferred
, pProps
, ppActual
);
963 if (!pProps
->cbAlign
|| (pProps
->cbAlign
% DEF_ALIGNMENT
) != 0)
964 pProps
->cbAlign
= DEF_ALIGNMENT
;
968 ALLOCATOR_PROPERTIES PropsActual
;
969 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, &PropsActual
);
970 /* FIXME: check we are still aligned */
973 IMemAllocator_AddRef(pPreferred
);
974 *ppActual
= pPreferred
;
975 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr
);
982 hr
= CoCreateInstance(&CLSID_MemoryAllocator
, NULL
, CLSCTX_INPROC
, &IID_IMemAllocator
, (LPVOID
*)&pPreferred
);
986 ALLOCATOR_PROPERTIES PropsActual
;
987 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, &PropsActual
);
988 /* FIXME: check we are still aligned */
991 *ppActual
= pPreferred
;
992 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr
);
1001 IMemAllocator_Release(pPreferred
);
1004 TRACE("-- %x\n", hr
);
1008 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
1009 * however, this would be quite complicated to do and may be a bit error prone */
1010 static HRESULT WINAPI
FileAsyncReader_Request(IAsyncReader
* iface
, IMediaSample
* pSample
, DWORD_PTR dwUser
)
1012 REFERENCE_TIME Start
;
1013 REFERENCE_TIME Stop
;
1014 DATAREQUEST
* pDataRq
;
1017 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1019 TRACE("(%p, %lx)\n", pSample
, dwUser
);
1021 /* check flushing state */
1022 if (This
->bFlushing
)
1023 return VFW_E_WRONG_STATE
;
1025 if (!(pDataRq
= CoTaskMemAlloc(sizeof(*pDataRq
))))
1028 /* get start and stop positions in bytes */
1030 hr
= IMediaSample_GetTime(pSample
, &Start
, &Stop
);
1033 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
1037 DWORD dwLength
= (DWORD
) BYTES_FROM_MEDIATIME(Stop
- Start
);
1039 pDataRq
->ovl
.u
.s
.Offset
= (DWORD
) BYTES_FROM_MEDIATIME(Start
);
1040 pDataRq
->ovl
.u
.s
.OffsetHigh
= (DWORD
)(BYTES_FROM_MEDIATIME(Start
) >> (sizeof(DWORD
) * 8));
1041 pDataRq
->ovl
.hEvent
= This
->hEvent
;
1042 pDataRq
->dwUserData
= dwUser
;
1043 pDataRq
->pNext
= NULL
;
1044 /* we violate traditional COM rules here by maintaining
1045 * a reference to the sample, but not calling AddRef, but
1046 * that's what MSDN says to do */
1047 pDataRq
->pSample
= pSample
;
1049 EnterCriticalSection(&This
->csList
);
1052 /* adds data request to end of list */
1053 queue(This
->pHead
, pDataRq
);
1055 This
->pHead
= pDataRq
;
1057 LeaveCriticalSection(&This
->csList
);
1059 /* this is definitely not how it is implemented on Win9x
1060 * as they do not support async reads on files, but it is
1061 * sooo much easier to use this than messing around with threads!
1063 if (!ReadFile(This
->hFile
, pBuffer
, dwLength
, NULL
, &pDataRq
->ovl
))
1064 hr
= HRESULT_FROM_WIN32(GetLastError());
1066 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1067 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1071 if (FAILED(hr
) && pDataRq
)
1073 EnterCriticalSection(&This
->csList
);
1075 DATAREQUEST
* pCurrent
;
1076 for (pCurrent
= This
->pHead
; pCurrent
&& pCurrent
->pNext
; pCurrent
= pCurrent
->pNext
)
1077 if (pCurrent
->pNext
== pDataRq
)
1079 pCurrent
->pNext
= pDataRq
->pNext
;
1083 LeaveCriticalSection(&This
->csList
);
1084 CoTaskMemFree(pDataRq
);
1087 TRACE("-- %x\n", hr
);
1091 static HRESULT WINAPI
FileAsyncReader_WaitForNext(IAsyncReader
* iface
, DWORD dwTimeout
, IMediaSample
** ppSample
, DWORD_PTR
* pdwUser
)
1095 DATAREQUEST
* pDataRq
= NULL
;
1096 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1098 TRACE("(%u, %p, %p)\n", dwTimeout
, ppSample
, pdwUser
);
1100 /* FIXME: we could do with improving this by waiting for an array of event handles
1101 * and then determining which one finished and removing that from the list, otherwise
1102 * we will end up waiting for longer than we should do, if a later request finishes
1103 * before an earlier one */
1108 if (!This
->bFlushing
)
1110 /* wait for the read to finish or timeout */
1111 if (WaitForSingleObject(This
->hEvent
, dwTimeout
) == WAIT_TIMEOUT
)
1117 EnterCriticalSection(&This
->csList
);
1119 pDataRq
= This
->pHead
;
1120 if (pDataRq
== NULL
)
1123 This
->pHead
= pDataRq
->pNext
;
1125 LeaveCriticalSection(&This
->csList
);
1128 if (SUCCEEDED(hr
) && !This
->bFlushing
)
1130 /* get any errors */
1131 if (!GetOverlappedResult(This
->hFile
, &pDataRq
->ovl
, &dwBytes
, FALSE
))
1132 hr
= HRESULT_FROM_WIN32(GetLastError());
1137 IMediaSample_SetActualDataLength(pDataRq
->pSample
, dwBytes
);
1138 *ppSample
= pDataRq
->pSample
;
1139 *pdwUser
= pDataRq
->dwUserData
;
1142 /* no need to close event handle since we will close it when the pin is destroyed */
1143 CoTaskMemFree(pDataRq
);
1145 /* Return the sample if flushing so it can be destroyed */
1146 if (This
->bFlushing
&& SUCCEEDED(hr
))
1148 hr
= VFW_E_WRONG_STATE
;
1149 IMediaSample_SetActualDataLength(pDataRq
->pSample
, 0);
1152 TRACE("-- %x\n", hr
);
1156 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
);
1158 static HRESULT WINAPI
FileAsyncReader_SyncReadAligned(IAsyncReader
* iface
, IMediaSample
* pSample
)
1161 REFERENCE_TIME tStart
;
1162 REFERENCE_TIME tStop
;
1165 TRACE("(%p)\n", pSample
);
1167 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
1170 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
1173 hr
= FileAsyncReader_SyncRead(iface
,
1174 BYTES_FROM_MEDIATIME(tStart
),
1175 (LONG
) BYTES_FROM_MEDIATIME(tStop
- tStart
),
1178 TRACE("-- %x\n", hr
);
1182 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
)
1186 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1188 TRACE("(%x%08x, %d, %p)\n", (ULONG
)(llPosition
>> 32), (ULONG
)llPosition
, lLength
, pBuffer
);
1190 ZeroMemory(&ovl
, sizeof(ovl
));
1192 ovl
.hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
1193 /* NOTE: llPosition is the actual byte position to start reading from */
1194 ovl
.u
.s
.Offset
= (DWORD
) llPosition
;
1195 ovl
.u
.s
.OffsetHigh
= (DWORD
) (llPosition
>> (sizeof(DWORD
) * 8));
1197 if (!ReadFile(This
->hFile
, pBuffer
, lLength
, NULL
, &ovl
))
1198 hr
= HRESULT_FROM_WIN32(GetLastError());
1200 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1207 if (!GetOverlappedResult(This
->hFile
, &ovl
, &dwBytesRead
, TRUE
))
1208 hr
= HRESULT_FROM_WIN32(GetLastError());
1211 CloseHandle(ovl
.hEvent
);
1213 TRACE("-- %x\n", hr
);
1217 static HRESULT WINAPI
FileAsyncReader_Length(IAsyncReader
* iface
, LONGLONG
* pTotal
, LONGLONG
* pAvailable
)
1221 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1223 TRACE("(%p, %p)\n", pTotal
, pAvailable
);
1225 if (((dwSizeLow
= GetFileSize(This
->hFile
, &dwSizeHigh
)) == -1) &&
1226 (GetLastError() != NO_ERROR
))
1227 return HRESULT_FROM_WIN32(GetLastError());
1229 *pTotal
= (LONGLONG
)dwSizeLow
| (LONGLONG
)dwSizeHigh
<< (sizeof(DWORD
) * 8);
1231 *pAvailable
= *pTotal
;
1236 static HRESULT WINAPI
FileAsyncReader_BeginFlush(IAsyncReader
* iface
)
1238 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1242 This
->bFlushing
= TRUE
;
1243 CancelIo(This
->hFile
);
1244 SetEvent(This
->hEvent
);
1246 /* FIXME: free list */
1251 static HRESULT WINAPI
FileAsyncReader_EndFlush(IAsyncReader
* iface
)
1253 FileAsyncReader
*This
= impl_from_IAsyncReader(iface
);
1257 This
->bFlushing
= FALSE
;
1262 static const IAsyncReaderVtbl FileAsyncReader_Vtbl
=
1264 FileAsyncReader_QueryInterface
,
1265 FileAsyncReader_AddRef
,
1266 FileAsyncReader_Release
,
1267 FileAsyncReader_RequestAllocator
,
1268 FileAsyncReader_Request
,
1269 FileAsyncReader_WaitForNext
,
1270 FileAsyncReader_SyncReadAligned
,
1271 FileAsyncReader_SyncRead
,
1272 FileAsyncReader_Length
,
1273 FileAsyncReader_BeginFlush
,
1274 FileAsyncReader_EndFlush
,