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 #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 struct IBaseFilterVtbl
* lpVtbl
;
43 const struct IFileSourceFilterVtbl
* lpVtblFSF
;
46 FILTER_INFO filterInfo
;
48 CRITICAL_SECTION csFilter
;
55 static const struct IBaseFilterVtbl AsyncReader_Vtbl
;
56 static const struct IFileSourceFilterVtbl FileSource_Vtbl
;
57 static const struct IAsyncReaderVtbl FileAsyncReader_Vtbl
;
59 static HRESULT
FileAsyncReader_Construct(HANDLE hFile
, IBaseFilter
* pBaseFilter
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
);
61 #define _IFileSourceFilter_Offset ((int)(&(((AsyncReader*)0)->lpVtblFSF)))
62 #define ICOM_THIS_From_IFileSourceFilter(impl, iface) impl* This = (impl*)(((char*)iface)-_IFileSourceFilter_Offset);
64 #define _IAsyncReader_Offset ((int)(&(((FileAsyncReader*)0)->lpVtblAR)))
65 #define ICOM_THIS_From_IAsyncReader(impl, iface) impl* This = (impl*)(((char*)iface)-_IAsyncReader_Offset);
67 static HRESULT
process_extensions(HKEY hkeyExtensions
, LPCOLESTR pszFileName
, GUID
* majorType
, GUID
* minorType
)
69 /* FIXME: implement */
73 static unsigned char byte_from_hex_char(WCHAR wHex
)
75 switch (tolowerW(wHex
))
94 return wHex
- 'a' + 10;
100 static HRESULT
process_pattern_string(LPCWSTR wszPatternString
, IAsyncReader
* pReader
)
110 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString
));
112 /* format: "offset, bytestocompare, mask, value" */
114 ulOffset
= strtolW(wszPatternString
, NULL
, 10);
116 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
119 wszPatternString
++; /* skip ',' */
121 ulBytes
= strtolW(wszPatternString
, NULL
, 10);
123 pbMask
= HeapAlloc(GetProcessHeap(), 0, ulBytes
);
124 pbValue
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, ulBytes
);
125 pbFile
= HeapAlloc(GetProcessHeap(), 0, ulBytes
);
127 /* default mask is match everything */
128 memset(pbMask
, 0xFF, ulBytes
);
130 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
133 wszPatternString
++; /* skip ',' */
137 for ( ; !isxdigitW(*wszPatternString
) && (*wszPatternString
!= ','); wszPatternString
++)
140 for (strpos
= 0; isxdigitW(*wszPatternString
) && (strpos
/2 < ulBytes
); wszPatternString
++, strpos
++)
142 if ((strpos
% 2) == 1) /* odd numbered position */
143 pbMask
[strpos
/ 2] |= byte_from_hex_char(*wszPatternString
);
145 pbMask
[strpos
/ 2] = byte_from_hex_char(*wszPatternString
) << 4;
148 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
151 wszPatternString
++; /* skip ',' */
156 for ( ; !isxdigitW(*wszPatternString
) && (*wszPatternString
!= ','); wszPatternString
++)
159 for (strpos
= 0; isxdigitW(*wszPatternString
) && (strpos
/2 < ulBytes
); wszPatternString
++, strpos
++)
161 if ((strpos
% 2) == 1) /* odd numbered position */
162 pbValue
[strpos
/ 2] |= byte_from_hex_char(*wszPatternString
);
164 pbValue
[strpos
/ 2] = byte_from_hex_char(*wszPatternString
) << 4;
169 hr
= IAsyncReader_SyncRead(pReader
, ulOffset
, ulBytes
, pbFile
);
174 for (i
= 0; i
< ulBytes
; i
++)
175 if ((pbFile
[i
] & pbMask
[i
]) != pbValue
[i
])
182 HeapFree(GetProcessHeap(), 0, pbMask
);
183 HeapFree(GetProcessHeap(), 0, pbValue
);
184 HeapFree(GetProcessHeap(), 0, pbFile
);
186 /* if we encountered no errors with this string, and there is a following tuple, then we
187 * have to match that as well to succeed */
188 if ((hr
== S_OK
) && (wszPatternString
= strchrW(wszPatternString
, ',')))
189 return process_pattern_string(wszPatternString
+ 1, pReader
);
194 static HRESULT
GetClassMediaFile(IAsyncReader
* pReader
, LPCOLESTR pszFileName
, GUID
* majorType
, GUID
* minorType
)
196 HKEY hkeyMediaType
= NULL
;
199 static const WCHAR wszMediaType
[] = {'M','e','d','i','a',' ','T','y','p','e',0};
201 CopyMemory(majorType
, &GUID_NULL
, sizeof(*majorType
));
202 CopyMemory(minorType
, &GUID_NULL
, sizeof(*minorType
));
204 hr
= HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_CLASSES_ROOT
, wszMediaType
, 0, KEY_READ
, &hkeyMediaType
));
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
);
318 pAsyncRead
->pszFileName
= NULL
;
319 pAsyncRead
->pmt
= NULL
;
321 *ppv
= (LPVOID
)pAsyncRead
;
323 TRACE("-- created at %p\n", pAsyncRead
);
328 /** IUnkown methods **/
330 static HRESULT WINAPI
AsyncReader_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
332 AsyncReader
*This
= (AsyncReader
*)iface
;
334 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
338 if (IsEqualIID(riid
, &IID_IUnknown
))
340 else if (IsEqualIID(riid
, &IID_IPersist
))
342 else if (IsEqualIID(riid
, &IID_IMediaFilter
))
344 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
346 else if (IsEqualIID(riid
, &IID_IFileSourceFilter
))
347 *ppv
= (LPVOID
)(&This
->lpVtblFSF
);
351 IUnknown_AddRef((IUnknown
*)(*ppv
));
355 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
357 return E_NOINTERFACE
;
360 static ULONG WINAPI
AsyncReader_AddRef(IBaseFilter
* iface
)
362 AsyncReader
*This
= (AsyncReader
*)iface
;
363 ULONG refCount
= InterlockedIncrement(&This
->refCount
);
365 TRACE("(%p/%p)->() AddRef from %ld\n", This
, iface
, refCount
- 1);
370 static ULONG WINAPI
AsyncReader_Release(IBaseFilter
* iface
)
372 AsyncReader
*This
= (AsyncReader
*)iface
;
373 ULONG refCount
= InterlockedDecrement(&This
->refCount
);
375 TRACE("(%p/%p)->() Release from %ld\n", This
, iface
, refCount
+ 1);
379 if (This
->pOutputPin
)
380 IPin_Release(This
->pOutputPin
);
381 DeleteCriticalSection(&This
->csFilter
);
390 /** IPersist methods **/
392 static HRESULT WINAPI
AsyncReader_GetClassID(IBaseFilter
* iface
, CLSID
* pClsid
)
394 TRACE("(%p)\n", pClsid
);
396 *pClsid
= CLSID_AsyncReader
;
401 /** IMediaFilter methods **/
403 static HRESULT WINAPI
AsyncReader_Stop(IBaseFilter
* iface
)
405 AsyncReader
*This
= (AsyncReader
*)iface
;
409 This
->state
= State_Stopped
;
414 static HRESULT WINAPI
AsyncReader_Pause(IBaseFilter
* iface
)
416 AsyncReader
*This
= (AsyncReader
*)iface
;
420 This
->state
= State_Paused
;
425 static HRESULT WINAPI
AsyncReader_Run(IBaseFilter
* iface
, REFERENCE_TIME tStart
)
427 AsyncReader
*This
= (AsyncReader
*)iface
;
429 TRACE("(%lx%08lx)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
);
431 This
->state
= State_Running
;
436 static HRESULT WINAPI
AsyncReader_GetState(IBaseFilter
* iface
, DWORD dwMilliSecsTimeout
, FILTER_STATE
*pState
)
438 AsyncReader
*This
= (AsyncReader
*)iface
;
440 TRACE("(%lu, %p)\n", dwMilliSecsTimeout
, pState
);
442 *pState
= This
->state
;
447 static HRESULT WINAPI
AsyncReader_SetSyncSource(IBaseFilter
* iface
, IReferenceClock
*pClock
)
449 /* AsyncReader *This = (AsyncReader *)iface;*/
451 TRACE("(%p)\n", pClock
);
456 static HRESULT WINAPI
AsyncReader_GetSyncSource(IBaseFilter
* iface
, IReferenceClock
**ppClock
)
458 /* AsyncReader *This = (AsyncReader *)iface;*/
460 TRACE("(%p)\n", ppClock
);
465 /** IBaseFilter methods **/
467 static HRESULT WINAPI
AsyncReader_EnumPins(IBaseFilter
* iface
, IEnumPins
**ppEnum
)
470 AsyncReader
*This
= (AsyncReader
*)iface
;
472 TRACE("(%p)\n", ppEnum
);
474 epd
.cPins
= This
->pOutputPin
? 1 : 0;
475 epd
.ppPins
= &This
->pOutputPin
;
476 return IEnumPinsImpl_Construct(&epd
, ppEnum
);
479 static HRESULT WINAPI
AsyncReader_FindPin(IBaseFilter
* iface
, LPCWSTR Id
, IPin
**ppPin
)
481 FIXME("(%s, %p)\n", debugstr_w(Id
), ppPin
);
486 static HRESULT WINAPI
AsyncReader_QueryFilterInfo(IBaseFilter
* iface
, FILTER_INFO
*pInfo
)
488 AsyncReader
*This
= (AsyncReader
*)iface
;
490 TRACE("(%p)\n", pInfo
);
492 strcpyW(pInfo
->achName
, This
->filterInfo
.achName
);
493 pInfo
->pGraph
= This
->filterInfo
.pGraph
;
496 IFilterGraph_AddRef(pInfo
->pGraph
);
501 static HRESULT WINAPI
AsyncReader_JoinFilterGraph(IBaseFilter
* iface
, IFilterGraph
*pGraph
, LPCWSTR pName
)
503 AsyncReader
*This
= (AsyncReader
*)iface
;
505 TRACE("(%p, %s)\n", pGraph
, debugstr_w(pName
));
508 strcpyW(This
->filterInfo
.achName
, pName
);
510 *This
->filterInfo
.achName
= 0;
511 This
->filterInfo
.pGraph
= pGraph
; /* NOTE: do NOT increase ref. count */
516 static HRESULT WINAPI
AsyncReader_QueryVendorInfo(IBaseFilter
* iface
, LPWSTR
*pVendorInfo
)
518 FIXME("(%p)\n", pVendorInfo
);
523 static const IBaseFilterVtbl AsyncReader_Vtbl
=
525 AsyncReader_QueryInterface
,
528 AsyncReader_GetClassID
,
532 AsyncReader_GetState
,
533 AsyncReader_SetSyncSource
,
534 AsyncReader_GetSyncSource
,
535 AsyncReader_EnumPins
,
537 AsyncReader_QueryFilterInfo
,
538 AsyncReader_JoinFilterGraph
,
539 AsyncReader_QueryVendorInfo
542 static HRESULT WINAPI
FileSource_QueryInterface(IFileSourceFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
544 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
546 return IBaseFilter_QueryInterface((IFileSourceFilter
*)&This
->lpVtbl
, riid
, ppv
);
549 static ULONG WINAPI
FileSource_AddRef(IFileSourceFilter
* iface
)
551 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
553 return IBaseFilter_AddRef((IFileSourceFilter
*)&This
->lpVtbl
);
556 static ULONG WINAPI
FileSource_Release(IFileSourceFilter
* iface
)
558 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
560 return IBaseFilter_Release((IFileSourceFilter
*)&This
->lpVtbl
);
563 static HRESULT WINAPI
FileSource_Load(IFileSourceFilter
* iface
, LPCOLESTR pszFileName
, const AM_MEDIA_TYPE
* pmt
)
567 IAsyncReader
* pReader
= NULL
;
568 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
570 TRACE("(%s, %p)\n", debugstr_w(pszFileName
), pmt
);
573 /* FIXME: check the sharing values that native uses */
574 hFile
= CreateFileW(pszFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, NULL
);
576 if (hFile
== INVALID_HANDLE_VALUE
)
578 return HRESULT_FROM_WIN32(GetLastError());
582 hr
= FileAsyncReader_Construct(hFile
, (IBaseFilter
*)&This
->lpVtbl
, &This
->csFilter
, &This
->pOutputPin
);
585 hr
= IPin_QueryInterface(This
->pOutputPin
, &IID_IAsyncReader
, (LPVOID
*)&pReader
);
587 /* store file name & media type */
590 This
->pszFileName
= CoTaskMemAlloc((strlenW(pszFileName
) + 1) * sizeof(WCHAR
));
591 strcpyW(This
->pszFileName
, pszFileName
);
592 This
->pmt
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
));
595 This
->pmt
->bFixedSizeSamples
= TRUE
;
596 This
->pmt
->bTemporalCompression
= FALSE
;
597 This
->pmt
->cbFormat
= 0;
598 This
->pmt
->pbFormat
= NULL
;
599 This
->pmt
->pUnk
= NULL
;
600 This
->pmt
->lSampleSize
= 0;
601 memcpy(&This
->pmt
->formattype
, &FORMAT_None
, sizeof(FORMAT_None
));
602 hr
= GetClassMediaFile(pReader
, pszFileName
, &This
->pmt
->majortype
, &This
->pmt
->subtype
);
605 CoTaskMemFree(This
->pmt
);
610 CopyMediaType(This
->pmt
, pmt
);
614 IAsyncReader_Release(pReader
);
618 if (This
->pOutputPin
)
620 IPin_Release(This
->pOutputPin
);
621 This
->pOutputPin
= NULL
;
623 if (This
->pszFileName
)
625 CoTaskMemFree(This
->pszFileName
);
626 This
->pszFileName
= NULL
;
631 /* FIXME: check return codes */
635 static HRESULT WINAPI
FileSource_GetCurFile(IFileSourceFilter
* iface
, LPOLESTR
* ppszFileName
, AM_MEDIA_TYPE
* pmt
)
637 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
639 TRACE("(%p, %p)\n", ppszFileName
, pmt
);
641 /* copy file name & media type if available, otherwise clear the outputs */
642 if (This
->pszFileName
)
644 *ppszFileName
= CoTaskMemAlloc((strlenW(This
->pszFileName
) + 1) * sizeof(WCHAR
));
645 strcpyW(*ppszFileName
, This
->pszFileName
);
648 *ppszFileName
= NULL
;
652 CopyMediaType(pmt
, This
->pmt
);
655 ZeroMemory(pmt
, sizeof(*pmt
));
660 static const IFileSourceFilterVtbl FileSource_Vtbl
=
662 FileSource_QueryInterface
,
666 FileSource_GetCurFile
670 /* the dwUserData passed back to user */
671 typedef struct DATAREQUEST
673 IMediaSample
* pSample
; /* sample passed to us by user */
674 DWORD_PTR dwUserData
; /* user data passed to us */
675 OVERLAPPED ovl
; /* our overlapped structure */
677 struct DATAREQUEST
* pNext
; /* next data request in list */
680 void queue(DATAREQUEST
* pHead
, DATAREQUEST
* pItem
)
682 DATAREQUEST
* pCurrent
;
683 for (pCurrent
= pHead
; pCurrent
->pNext
; pCurrent
= pCurrent
->pNext
)
685 pCurrent
->pNext
= pItem
;
688 typedef struct FileAsyncReader
691 const struct IAsyncReaderVtbl
* lpVtblAR
;
696 DATAREQUEST
* pHead
; /* head of data request list */
697 CRITICAL_SECTION csList
; /* critical section to protect operations on list */
700 static HRESULT
AcceptProcAFR(LPVOID iface
, const AM_MEDIA_TYPE
*pmt
)
702 AsyncReader
*This
= (AsyncReader
*)iface
;
704 FIXME("(%p, %p)\n", iface
, pmt
);
706 if (IsEqualGUID(&pmt
->majortype
, &This
->pmt
->majortype
) &&
707 IsEqualGUID(&pmt
->subtype
, &This
->pmt
->subtype
) &&
708 IsEqualGUID(&pmt
->formattype
, &FORMAT_None
))
714 /* overriden pin functions */
716 static HRESULT WINAPI
FileAsyncReaderPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
718 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
719 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
723 if (IsEqualIID(riid
, &IID_IUnknown
))
725 else if (IsEqualIID(riid
, &IID_IPin
))
727 else if (IsEqualIID(riid
, &IID_IAsyncReader
))
728 *ppv
= (LPVOID
)&This
->lpVtblAR
;
732 IUnknown_AddRef((IUnknown
*)(*ppv
));
736 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
738 return E_NOINTERFACE
;
741 static ULONG WINAPI
FileAsyncReaderPin_Release(IPin
* iface
)
743 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
744 ULONG refCount
= InterlockedDecrement(&This
->pin
.pin
.refCount
);
750 DATAREQUEST
* pCurrent
;
752 for (pCurrent
= This
->pHead
; pCurrent
; pCurrent
= pNext
)
754 pNext
= pCurrent
->pNext
;
755 CoTaskMemFree(pCurrent
);
757 CloseHandle(This
->hFile
);
758 CloseHandle(This
->hEvent
);
765 static HRESULT WINAPI
FileAsyncReaderPin_EnumMediaTypes(IPin
* iface
, IEnumMediaTypes
** ppEnum
)
767 ENUMMEDIADETAILS emd
;
768 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
770 TRACE("(%p)\n", ppEnum
);
773 emd
.pMediaTypes
= ((AsyncReader
*)This
->pin
.pin
.pinInfo
.pFilter
)->pmt
;
775 return IEnumMediaTypesImpl_Construct(&emd
, ppEnum
);
778 static const IPinVtbl FileAsyncReaderPin_Vtbl
=
780 FileAsyncReaderPin_QueryInterface
,
782 FileAsyncReaderPin_Release
,
784 OutputPin_ReceiveConnection
,
786 IPinImpl_ConnectedTo
,
787 IPinImpl_ConnectionMediaType
,
788 IPinImpl_QueryPinInfo
,
789 IPinImpl_QueryDirection
,
791 IPinImpl_QueryAccept
,
792 FileAsyncReaderPin_EnumMediaTypes
,
793 IPinImpl_QueryInternalConnections
,
794 OutputPin_EndOfStream
,
795 OutputPin_BeginFlush
,
800 /* Function called as a helper to IPin_Connect */
801 /* specific AM_MEDIA_TYPE - it cannot be NULL */
802 /* this differs from standard OutputPin_ConnectSpecific only in that it
803 * doesn't need the IMemInputPin interface on the receiving pin */
804 static HRESULT
FileAsyncReaderPin_ConnectSpecific(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
806 OutputPin
*This
= (OutputPin
*)iface
;
809 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
810 dump_AM_MEDIA_TYPE(pmt
);
812 /* FIXME: call queryacceptproc */
814 This
->pin
.pConnectedTo
= pReceivePin
;
815 IPin_AddRef(pReceivePin
);
816 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
818 hr
= IPin_ReceiveConnection(pReceivePin
, iface
, pmt
);
822 IPin_Release(This
->pin
.pConnectedTo
);
823 This
->pin
.pConnectedTo
= NULL
;
824 DeleteMediaType(&This
->pin
.mtCurrent
);
827 TRACE(" -- %lx\n", hr
);
831 static HRESULT
FileAsyncReader_Construct(HANDLE hFile
, IBaseFilter
* pBaseFilter
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
833 FileAsyncReader
* pPinImpl
;
838 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
841 return E_OUTOFMEMORY
;
843 piOutput
.dir
= PINDIR_OUTPUT
;
844 piOutput
.pFilter
= pBaseFilter
;
845 strcpyW(piOutput
.achName
, wszOutputPinName
);
847 if (SUCCEEDED(OutputPin_Init(&piOutput
, NULL
, pBaseFilter
, AcceptProcAFR
, pCritSec
, &pPinImpl
->pin
)))
849 pPinImpl
->pin
.pin
.lpVtbl
= &FileAsyncReaderPin_Vtbl
;
850 pPinImpl
->lpVtblAR
= &FileAsyncReader_Vtbl
;
851 pPinImpl
->hFile
= hFile
;
852 pPinImpl
->hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
853 pPinImpl
->bFlushing
= FALSE
;
854 pPinImpl
->pHead
= NULL
;
855 pPinImpl
->pin
.pConnectSpecific
= FileAsyncReaderPin_ConnectSpecific
;
856 InitializeCriticalSection(&pPinImpl
->csList
);
858 *ppPin
= (IPin
*)(&pPinImpl
->pin
.pin
.lpVtbl
);
866 static HRESULT WINAPI
FileAsyncReader_QueryInterface(IAsyncReader
* iface
, REFIID riid
, LPVOID
* ppv
)
868 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
870 return IPin_QueryInterface((IPin
*)This
, riid
, ppv
);
873 static ULONG WINAPI
FileAsyncReader_AddRef(IAsyncReader
* iface
)
875 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
877 return IPin_AddRef((IPin
*)This
);
880 static ULONG WINAPI
FileAsyncReader_Release(IAsyncReader
* iface
)
882 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
884 return IPin_Release((IPin
*)This
);
887 #define DEF_ALIGNMENT 1
889 static HRESULT WINAPI
FileAsyncReader_RequestAllocator(IAsyncReader
* iface
, IMemAllocator
* pPreferred
, ALLOCATOR_PROPERTIES
* pProps
, IMemAllocator
** ppActual
)
893 TRACE("(%p, %p, %p)\n", pPreferred
, pProps
, ppActual
);
895 if (!pProps
->cbAlign
|| (pProps
->cbAlign
% DEF_ALIGNMENT
) != 0)
896 pProps
->cbAlign
= DEF_ALIGNMENT
;
900 ALLOCATOR_PROPERTIES PropsActual
;
901 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, &PropsActual
);
902 /* FIXME: check we are still aligned */
905 IMemAllocator_AddRef(pPreferred
);
906 *ppActual
= pPreferred
;
907 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr
);
914 hr
= CoCreateInstance(&CLSID_MemoryAllocator
, NULL
, CLSCTX_INPROC
, &IID_IMemAllocator
, (LPVOID
*)&pPreferred
);
918 ALLOCATOR_PROPERTIES PropsActual
;
919 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, &PropsActual
);
920 /* FIXME: check we are still aligned */
923 IMemAllocator_AddRef(pPreferred
);
924 *ppActual
= pPreferred
;
925 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr
);
934 IMemAllocator_Release(pPreferred
);
937 TRACE("-- %lx\n", hr
);
941 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
942 * however, this would be quite complicated to do and may be a bit error prone */
943 static HRESULT WINAPI
FileAsyncReader_Request(IAsyncReader
* iface
, IMediaSample
* pSample
, DWORD_PTR dwUser
)
945 REFERENCE_TIME Start
;
947 DATAREQUEST
* pDataRq
;
950 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
952 TRACE("(%p, %lx)\n", pSample
, dwUser
);
954 /* check flushing state */
956 return VFW_E_WRONG_STATE
;
958 if (!(pDataRq
= CoTaskMemAlloc(sizeof(*pDataRq
))))
961 /* get start and stop positions in bytes */
963 hr
= IMediaSample_GetTime(pSample
, &Start
, &Stop
);
966 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
970 DWORD dwLength
= (DWORD
) BYTES_FROM_MEDIATIME(Stop
- Start
);
972 pDataRq
->ovl
.u
.s
.Offset
= (DWORD
) BYTES_FROM_MEDIATIME(Start
);
973 pDataRq
->ovl
.u
.s
.OffsetHigh
= (DWORD
)(BYTES_FROM_MEDIATIME(Start
) >> (sizeof(DWORD
) * 8));
974 pDataRq
->ovl
.hEvent
= This
->hEvent
;
975 pDataRq
->dwUserData
= dwUser
;
976 pDataRq
->pNext
= NULL
;
977 /* we violate traditional COM rules here by maintaining
978 * a reference to the sample, but not calling AddRef, but
979 * that's what MSDN says to do */
980 pDataRq
->pSample
= pSample
;
982 EnterCriticalSection(&This
->csList
);
985 /* adds data request to end of list */
986 queue(This
->pHead
, pDataRq
);
988 This
->pHead
= pDataRq
;
990 LeaveCriticalSection(&This
->csList
);
992 /* this is definitely not how it is implemented on Win9x
993 * as they do not support async reads on files, but it is
994 * sooo much easier to use this than messing around with threads!
996 if (!ReadFile(This
->hFile
, pBuffer
, dwLength
, NULL
, &pDataRq
->ovl
))
997 hr
= HRESULT_FROM_WIN32(GetLastError());
999 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1000 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1004 if (FAILED(hr
) && pDataRq
)
1006 EnterCriticalSection(&This
->csList
);
1008 DATAREQUEST
* pCurrent
;
1009 for (pCurrent
= This
->pHead
; pCurrent
&& pCurrent
->pNext
; pCurrent
= pCurrent
->pNext
)
1010 if (pCurrent
->pNext
== pDataRq
)
1012 pCurrent
->pNext
= pDataRq
->pNext
;
1016 LeaveCriticalSection(&This
->csList
);
1017 CoTaskMemFree(pDataRq
);
1020 TRACE("-- %lx\n", hr
);
1024 static HRESULT WINAPI
FileAsyncReader_WaitForNext(IAsyncReader
* iface
, DWORD dwTimeout
, IMediaSample
** ppSample
, DWORD_PTR
* pdwUser
)
1027 DATAREQUEST
* pDataRq
= NULL
;
1028 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1030 TRACE("(%lu, %p, %p)\n", dwTimeout
, ppSample
, pdwUser
);
1032 /* FIXME: we could do with improving this by waiting for an array of event handles
1033 * and then determining which one finished and removing that from the list, otherwise
1034 * we will end up waiting for longer than we should do, if a later request finishes
1035 * before an earlier one */
1040 /* we return immediately if flushing */
1041 if (This
->bFlushing
)
1042 hr
= VFW_E_WRONG_STATE
;
1046 /* wait for the read to finish or timeout */
1047 if (WaitForSingleObject(This
->hEvent
, dwTimeout
) == WAIT_TIMEOUT
)
1052 EnterCriticalSection(&This
->csList
);
1054 pDataRq
= This
->pHead
;
1055 if (pDataRq
== NULL
)
1058 This
->pHead
= pDataRq
->pNext
;
1060 LeaveCriticalSection(&This
->csList
);
1066 /* get any errors */
1067 if (!GetOverlappedResult(This
->hFile
, &pDataRq
->ovl
, &dwBytes
, FALSE
))
1068 hr
= HRESULT_FROM_WIN32(GetLastError());
1073 *ppSample
= pDataRq
->pSample
;
1074 *pdwUser
= pDataRq
->dwUserData
;
1080 /* no need to close event handle since we will close it when the pin is destroyed */
1081 CoTaskMemFree(pDataRq
);
1084 TRACE("-- %lx\n", hr
);
1088 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
);
1090 static HRESULT WINAPI
FileAsyncReader_SyncReadAligned(IAsyncReader
* iface
, IMediaSample
* pSample
)
1093 REFERENCE_TIME tStart
;
1094 REFERENCE_TIME tStop
;
1097 TRACE("(%p)\n", pSample
);
1099 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
1102 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
1105 hr
= FileAsyncReader_SyncRead(iface
,
1106 BYTES_FROM_MEDIATIME(tStart
),
1107 (LONG
) BYTES_FROM_MEDIATIME(tStop
- tStart
),
1110 TRACE("-- %lx\n", hr
);
1114 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
)
1118 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1120 TRACE("(%lx%08lx, %ld, %p)\n", (ULONG
)(llPosition
>> 32), (ULONG
)llPosition
, lLength
, pBuffer
);
1122 ZeroMemory(&ovl
, sizeof(ovl
));
1124 ovl
.hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
1125 /* NOTE: llPosition is the actual byte position to start reading from */
1126 ovl
.u
.s
.Offset
= (DWORD
) llPosition
;
1127 ovl
.u
.s
.OffsetHigh
= (DWORD
) (llPosition
>> (sizeof(DWORD
) * 8));
1129 if (!ReadFile(This
->hFile
, pBuffer
, lLength
, NULL
, &ovl
))
1130 hr
= HRESULT_FROM_WIN32(GetLastError());
1132 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1139 if (!GetOverlappedResult(This
->hFile
, &ovl
, &dwBytesRead
, TRUE
))
1140 hr
= HRESULT_FROM_WIN32(GetLastError());
1143 CloseHandle(ovl
.hEvent
);
1145 TRACE("-- %lx\n", hr
);
1149 static HRESULT WINAPI
FileAsyncReader_Length(IAsyncReader
* iface
, LONGLONG
* pTotal
, LONGLONG
* pAvailable
)
1153 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1155 TRACE("(%p, %p)\n", pTotal
, pAvailable
);
1157 if (((dwSizeLow
= GetFileSize(This
->hFile
, &dwSizeHigh
)) == -1) &&
1158 (GetLastError() != NO_ERROR
))
1159 return HRESULT_FROM_WIN32(GetLastError());
1161 *pTotal
= (LONGLONG
)dwSizeLow
| (LONGLONG
)dwSizeHigh
<< (sizeof(DWORD
) * 8);
1163 *pAvailable
= *pTotal
;
1168 static HRESULT WINAPI
FileAsyncReader_BeginFlush(IAsyncReader
* iface
)
1170 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1174 This
->bFlushing
= TRUE
;
1175 CancelIo(This
->hFile
);
1176 SetEvent(This
->hEvent
);
1178 /* FIXME: free list */
1183 static HRESULT WINAPI
FileAsyncReader_EndFlush(IAsyncReader
* iface
)
1185 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1189 This
->bFlushing
= FALSE
;
1194 static const IAsyncReaderVtbl FileAsyncReader_Vtbl
=
1196 FileAsyncReader_QueryInterface
,
1197 FileAsyncReader_AddRef
,
1198 FileAsyncReader_Release
,
1199 FileAsyncReader_RequestAllocator
,
1200 FileAsyncReader_Request
,
1201 FileAsyncReader_WaitForNext
,
1202 FileAsyncReader_SyncReadAligned
,
1203 FileAsyncReader_SyncRead
,
1204 FileAsyncReader_Length
,
1205 FileAsyncReader_BeginFlush
,
1206 FileAsyncReader_EndFlush
,