4 * Copyright 2003 Robert Shearman
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "quartz_private.h"
23 #include "wine/debug.h"
24 #include "wine/unicode.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(quartz
);
35 static const WCHAR wszOutputPinName
[] = { 'O','u','t','p','u','t',0 };
37 typedef struct AsyncReader
39 const struct IBaseFilterVtbl
* lpVtbl
;
40 const struct IFileSourceFilterVtbl
* lpVtblFSF
;
43 FILTER_INFO filterInfo
;
45 CRITICAL_SECTION csFilter
;
52 static const struct IBaseFilterVtbl AsyncReader_Vtbl
;
53 static const struct IFileSourceFilterVtbl FileSource_Vtbl
;
54 static const struct IAsyncReaderVtbl FileAsyncReader_Vtbl
;
56 static HRESULT
FileAsyncReader_Construct(HANDLE hFile
, IBaseFilter
* pBaseFilter
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
);
58 #define _IFileSourceFilter_Offset ((int)(&(((AsyncReader*)0)->lpVtblFSF)))
59 #define ICOM_THIS_From_IFileSourceFilter(impl, iface) impl* This = (impl*)(((char*)iface)-_IFileSourceFilter_Offset);
61 #define _IAsyncReader_Offset ((int)(&(((FileAsyncReader*)0)->lpVtblAR)))
62 #define ICOM_THIS_From_IAsyncReader(impl, iface) impl* This = (impl*)(((char*)iface)-_IAsyncReader_Offset);
64 static HRESULT
process_extensions(HKEY hkeyExtensions
, LPCOLESTR pszFileName
, GUID
* majorType
, GUID
* minorType
)
66 /* FIXME: implement */
70 static unsigned char byte_from_hex_char(WCHAR wHex
)
72 switch (tolowerW(wHex
))
91 return wHex
- 'a' + 10;
97 static HRESULT
process_pattern_string(LPCWSTR wszPatternString
, IAsyncReader
* pReader
)
107 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString
));
109 /* format: "offset, bytestocompare, mask, value" */
111 ulOffset
= strtolW(wszPatternString
, NULL
, 10);
113 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
116 wszPatternString
++; /* skip ',' */
118 ulBytes
= strtolW(wszPatternString
, NULL
, 10);
120 pbMask
= HeapAlloc(GetProcessHeap(), 0, ulBytes
);
121 pbValue
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, ulBytes
);
122 pbFile
= HeapAlloc(GetProcessHeap(), 0, ulBytes
);
124 /* default mask is match everything */
125 memset(pbMask
, 0xFF, ulBytes
);
127 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
130 wszPatternString
++; /* skip ',' */
134 for ( ; !isxdigitW(*wszPatternString
) && (*wszPatternString
!= ','); wszPatternString
++)
137 for (strpos
= 0; isxdigitW(*wszPatternString
) && (strpos
/2 < ulBytes
); wszPatternString
++, strpos
++)
139 if ((strpos
% 2) == 1) /* odd numbered position */
140 pbMask
[strpos
/ 2] |= byte_from_hex_char(*wszPatternString
);
142 pbMask
[strpos
/ 2] = byte_from_hex_char(*wszPatternString
) << 4;
145 if (!(wszPatternString
= strchrW(wszPatternString
, ',')))
148 wszPatternString
++; /* skip ',' */
153 for ( ; !isxdigitW(*wszPatternString
) && (*wszPatternString
!= ','); wszPatternString
++)
156 for (strpos
= 0; isxdigitW(*wszPatternString
) && (strpos
/2 < ulBytes
); wszPatternString
++, strpos
++)
158 if ((strpos
% 2) == 1) /* odd numbered position */
159 pbValue
[strpos
/ 2] |= byte_from_hex_char(*wszPatternString
);
161 pbValue
[strpos
/ 2] = byte_from_hex_char(*wszPatternString
) << 4;
166 hr
= IAsyncReader_SyncRead(pReader
, ulOffset
, ulBytes
, pbFile
);
171 for (i
= 0; i
< ulBytes
; i
++)
172 if ((pbFile
[i
] & pbMask
[i
]) != pbValue
[i
])
179 HeapFree(GetProcessHeap(), 0, pbMask
);
180 HeapFree(GetProcessHeap(), 0, pbValue
);
181 HeapFree(GetProcessHeap(), 0, pbFile
);
183 /* if we encountered no errors with this string, and there is a following tuple, then we
184 * have to match that as well to succeed */
185 if ((hr
== S_OK
) && (wszPatternString
= strchrW(wszPatternString
, ',')))
186 return process_pattern_string(wszPatternString
+ 1, pReader
);
191 static HRESULT
GetClassMediaFile(IAsyncReader
* pReader
, LPCOLESTR pszFileName
, GUID
* majorType
, GUID
* minorType
)
193 HKEY hkeyMediaType
= NULL
;
196 static const WCHAR wszMediaType
[] = {'M','e','d','i','a',' ','T','y','p','e',0};
198 CopyMemory(majorType
, &GUID_NULL
, sizeof(*majorType
));
199 CopyMemory(minorType
, &GUID_NULL
, sizeof(*minorType
));
201 hr
= HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_CLASSES_ROOT
, wszMediaType
, 0, KEY_READ
, &hkeyMediaType
));
207 for (indexMajor
= 0; !bFound
; indexMajor
++)
210 WCHAR wszMajorKeyName
[CHARS_IN_GUID
];
211 DWORD dwKeyNameLength
= sizeof(wszMajorKeyName
) / sizeof(wszMajorKeyName
[0]);
212 static const WCHAR wszExtensions
[] = {'E','x','t','e','n','s','i','o','n','s',0};
214 if (RegEnumKeyExW(hkeyMediaType
, indexMajor
, wszMajorKeyName
, &dwKeyNameLength
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
216 if (RegOpenKeyExW(hkeyMediaType
, wszMajorKeyName
, 0, KEY_READ
, &hkeyMajor
) != ERROR_SUCCESS
)
218 TRACE("%s\n", debugstr_w(wszMajorKeyName
));
219 if (!strcmpW(wszExtensions
, wszMajorKeyName
))
221 if (process_extensions(hkeyMajor
, pszFileName
, majorType
, minorType
) == S_OK
)
228 for (indexMinor
= 0; !bFound
; indexMinor
++)
231 WCHAR wszMinorKeyName
[CHARS_IN_GUID
];
232 DWORD dwMinorKeyNameLen
= sizeof(wszMinorKeyName
) / sizeof(wszMinorKeyName
[0]);
236 if (RegEnumKeyExW(hkeyMajor
, indexMinor
, wszMinorKeyName
, &dwMinorKeyNameLen
, NULL
, NULL
, NULL
, NULL
) != ERROR_SUCCESS
)
239 if (RegOpenKeyExW(hkeyMajor
, wszMinorKeyName
, 0, KEY_READ
, &hkeyMinor
) != ERROR_SUCCESS
)
242 TRACE("\t%s\n", debugstr_w(wszMinorKeyName
));
244 if (RegQueryInfoKeyW(hkeyMinor
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, &maxValueLen
, NULL
, NULL
) != ERROR_SUCCESS
)
247 for (indexValue
= 0; !bFound
; indexValue
++)
250 WCHAR wszValueName
[14]; /* longest name we should encounter will be "Source Filter" */
251 LPWSTR wszPatternString
= HeapAlloc(GetProcessHeap(), 0, maxValueLen
);
252 DWORD dwValueNameLen
= sizeof(wszValueName
) / sizeof(wszValueName
[0]); /* remember this is in chars */
253 DWORD dwDataLen
= maxValueLen
; /* remember this is in bytes */
254 static const WCHAR wszSourceFilter
[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
257 if ((temp
= RegEnumValueW(hkeyMinor
, indexValue
, wszValueName
, &dwValueNameLen
, NULL
, &dwType
, (LPBYTE
)wszPatternString
, &dwDataLen
)) != ERROR_SUCCESS
)
259 HeapFree(GetProcessHeap(), 0, wszPatternString
);
263 /* if it is not the source filter value */
264 if (strcmpW(wszValueName
, wszSourceFilter
))
266 if (process_pattern_string(wszPatternString
, pReader
) == S_OK
)
268 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName
, majorType
)) &&
269 SUCCEEDED(CLSIDFromString(wszMinorKeyName
, minorType
)))
273 HeapFree(GetProcessHeap(), 0, wszPatternString
);
275 CloseHandle(hkeyMinor
);
278 CloseHandle(hkeyMajor
);
281 CloseHandle(hkeyMediaType
);
283 if (SUCCEEDED(hr
) && !bFound
)
285 ERR("Media class not found\n");
289 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType
), qzdebugstr_guid(minorType
));
294 HRESULT
AsyncReader_create(IUnknown
* pUnkOuter
, LPVOID
* ppv
)
296 AsyncReader
*pAsyncRead
;
299 return CLASS_E_NOAGGREGATION
;
301 pAsyncRead
= CoTaskMemAlloc(sizeof(AsyncReader
));
304 return E_OUTOFMEMORY
;
306 pAsyncRead
->lpVtbl
= &AsyncReader_Vtbl
;
307 pAsyncRead
->lpVtblFSF
= &FileSource_Vtbl
;
308 pAsyncRead
->refCount
= 1;
309 pAsyncRead
->filterInfo
.achName
[0] = '\0';
310 pAsyncRead
->filterInfo
.pGraph
= NULL
;
311 pAsyncRead
->pOutputPin
= NULL
;
313 InitializeCriticalSection(&pAsyncRead
->csFilter
);
315 pAsyncRead
->pszFileName
= NULL
;
316 pAsyncRead
->pmt
= NULL
;
318 *ppv
= (LPVOID
)pAsyncRead
;
320 TRACE("-- created at %p\n", pAsyncRead
);
325 /** IUnkown methods **/
327 static HRESULT WINAPI
AsyncReader_QueryInterface(IBaseFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
329 AsyncReader
*This
= (AsyncReader
*)iface
;
331 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
335 if (IsEqualIID(riid
, &IID_IUnknown
))
337 else if (IsEqualIID(riid
, &IID_IPersist
))
339 else if (IsEqualIID(riid
, &IID_IMediaFilter
))
341 else if (IsEqualIID(riid
, &IID_IBaseFilter
))
343 else if (IsEqualIID(riid
, &IID_IFileSourceFilter
))
344 *ppv
= (LPVOID
)(&This
->lpVtblFSF
);
348 IUnknown_AddRef((IUnknown
*)(*ppv
));
352 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
354 return E_NOINTERFACE
;
357 static ULONG WINAPI
AsyncReader_AddRef(IBaseFilter
* iface
)
359 AsyncReader
*This
= (AsyncReader
*)iface
;
363 return InterlockedIncrement(&This
->refCount
);
366 static ULONG WINAPI
AsyncReader_Release(IBaseFilter
* iface
)
368 AsyncReader
*This
= (AsyncReader
*)iface
;
372 if (!InterlockedDecrement(&This
->refCount
))
374 if (This
->pOutputPin
)
375 IPin_Release(This
->pOutputPin
);
376 DeleteCriticalSection(&This
->csFilter
);
382 return This
->refCount
;
385 /** IPersist methods **/
387 static HRESULT WINAPI
AsyncReader_GetClassID(IBaseFilter
* iface
, CLSID
* pClsid
)
389 TRACE("(%p)\n", pClsid
);
391 *pClsid
= CLSID_AsyncReader
;
396 /** IMediaFilter methods **/
398 static HRESULT WINAPI
AsyncReader_Stop(IBaseFilter
* iface
)
400 AsyncReader
*This
= (AsyncReader
*)iface
;
404 This
->state
= State_Stopped
;
409 static HRESULT WINAPI
AsyncReader_Pause(IBaseFilter
* iface
)
411 AsyncReader
*This
= (AsyncReader
*)iface
;
415 This
->state
= State_Paused
;
420 static HRESULT WINAPI
AsyncReader_Run(IBaseFilter
* iface
, REFERENCE_TIME tStart
)
422 AsyncReader
*This
= (AsyncReader
*)iface
;
424 TRACE("(%lx%08lx)\n", (ULONG
)(tStart
>> 32), (ULONG
)tStart
);
426 This
->state
= State_Running
;
431 static HRESULT WINAPI
AsyncReader_GetState(IBaseFilter
* iface
, DWORD dwMilliSecsTimeout
, FILTER_STATE
*pState
)
433 AsyncReader
*This
= (AsyncReader
*)iface
;
435 TRACE("(%lu, %p)\n", dwMilliSecsTimeout
, pState
);
437 *pState
= This
->state
;
442 static HRESULT WINAPI
AsyncReader_SetSyncSource(IBaseFilter
* iface
, IReferenceClock
*pClock
)
444 /* AsyncReader *This = (AsyncReader *)iface;*/
446 TRACE("(%p)\n", pClock
);
451 static HRESULT WINAPI
AsyncReader_GetSyncSource(IBaseFilter
* iface
, IReferenceClock
**ppClock
)
453 /* AsyncReader *This = (AsyncReader *)iface;*/
455 TRACE("(%p)\n", ppClock
);
460 /** IBaseFilter methods **/
462 static HRESULT WINAPI
AsyncReader_EnumPins(IBaseFilter
* iface
, IEnumPins
**ppEnum
)
465 AsyncReader
*This
= (AsyncReader
*)iface
;
467 TRACE("(%p)\n", ppEnum
);
469 epd
.cPins
= This
->pOutputPin
? 1 : 0;
470 epd
.ppPins
= &This
->pOutputPin
;
471 return IEnumPinsImpl_Construct(&epd
, ppEnum
);
474 static HRESULT WINAPI
AsyncReader_FindPin(IBaseFilter
* iface
, LPCWSTR Id
, IPin
**ppPin
)
476 FIXME("(%s, %p)\n", debugstr_w(Id
), ppPin
);
481 static HRESULT WINAPI
AsyncReader_QueryFilterInfo(IBaseFilter
* iface
, FILTER_INFO
*pInfo
)
483 AsyncReader
*This
= (AsyncReader
*)iface
;
485 TRACE("(%p)\n", pInfo
);
487 strcpyW(pInfo
->achName
, This
->filterInfo
.achName
);
488 pInfo
->pGraph
= This
->filterInfo
.pGraph
;
491 IFilterGraph_AddRef(pInfo
->pGraph
);
496 static HRESULT WINAPI
AsyncReader_JoinFilterGraph(IBaseFilter
* iface
, IFilterGraph
*pGraph
, LPCWSTR pName
)
498 AsyncReader
*This
= (AsyncReader
*)iface
;
500 TRACE("(%p, %s)\n", pGraph
, debugstr_w(pName
));
503 strcpyW(This
->filterInfo
.achName
, pName
);
505 *This
->filterInfo
.achName
= 0;
506 This
->filterInfo
.pGraph
= pGraph
; /* NOTE: do NOT increase ref. count */
511 static HRESULT WINAPI
AsyncReader_QueryVendorInfo(IBaseFilter
* iface
, LPWSTR
*pVendorInfo
)
513 FIXME("(%p)\n", pVendorInfo
);
518 static const IBaseFilterVtbl AsyncReader_Vtbl
=
520 AsyncReader_QueryInterface
,
523 AsyncReader_GetClassID
,
527 AsyncReader_GetState
,
528 AsyncReader_SetSyncSource
,
529 AsyncReader_GetSyncSource
,
530 AsyncReader_EnumPins
,
532 AsyncReader_QueryFilterInfo
,
533 AsyncReader_JoinFilterGraph
,
534 AsyncReader_QueryVendorInfo
537 static HRESULT WINAPI
FileSource_QueryInterface(IFileSourceFilter
* iface
, REFIID riid
, LPVOID
* ppv
)
539 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
541 return IBaseFilter_QueryInterface((IFileSourceFilter
*)&This
->lpVtbl
, riid
, ppv
);
544 static ULONG WINAPI
FileSource_AddRef(IFileSourceFilter
* iface
)
546 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
548 return IBaseFilter_AddRef((IFileSourceFilter
*)&This
->lpVtbl
);
551 static ULONG WINAPI
FileSource_Release(IFileSourceFilter
* iface
)
553 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
555 return IBaseFilter_Release((IFileSourceFilter
*)&This
->lpVtbl
);
558 static HRESULT WINAPI
FileSource_Load(IFileSourceFilter
* iface
, LPCOLESTR pszFileName
, const AM_MEDIA_TYPE
* pmt
)
562 IAsyncReader
* pReader
= NULL
;
563 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
565 TRACE("(%s, %p)\n", debugstr_w(pszFileName
), pmt
);
568 /* FIXME: check the sharing values that native uses */
569 hFile
= CreateFileW(pszFileName
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_FLAG_OVERLAPPED
, NULL
);
571 if (hFile
== INVALID_HANDLE_VALUE
)
573 return HRESULT_FROM_WIN32(GetLastError());
577 hr
= FileAsyncReader_Construct(hFile
, (IBaseFilter
*)&This
->lpVtbl
, &This
->csFilter
, &This
->pOutputPin
);
580 hr
= IPin_QueryInterface(This
->pOutputPin
, &IID_IAsyncReader
, (LPVOID
*)&pReader
);
582 /* store file name & media type */
585 This
->pszFileName
= CoTaskMemAlloc((strlenW(pszFileName
) + 1) * sizeof(WCHAR
));
586 strcpyW(This
->pszFileName
, pszFileName
);
587 This
->pmt
= CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE
));
590 This
->pmt
->bFixedSizeSamples
= TRUE
;
591 This
->pmt
->bTemporalCompression
= FALSE
;
592 This
->pmt
->cbFormat
= 0;
593 This
->pmt
->pbFormat
= NULL
;
594 This
->pmt
->pUnk
= NULL
;
595 This
->pmt
->lSampleSize
= 0;
596 memcpy(&This
->pmt
->formattype
, &FORMAT_None
, sizeof(FORMAT_None
));
597 hr
= GetClassMediaFile(pReader
, pszFileName
, &This
->pmt
->majortype
, &This
->pmt
->subtype
);
600 CoTaskMemFree(This
->pmt
);
605 CopyMediaType(This
->pmt
, pmt
);
609 IAsyncReader_Release(pReader
);
613 if (This
->pOutputPin
)
615 IPin_Release(This
->pOutputPin
);
616 This
->pOutputPin
= NULL
;
618 if (This
->pszFileName
)
620 CoTaskMemFree(This
->pszFileName
);
621 This
->pszFileName
= NULL
;
626 /* FIXME: check return codes */
630 static HRESULT WINAPI
FileSource_GetCurFile(IFileSourceFilter
* iface
, LPOLESTR
* ppszFileName
, AM_MEDIA_TYPE
* pmt
)
632 ICOM_THIS_From_IFileSourceFilter(AsyncReader
, iface
);
634 TRACE("(%p, %p)\n", ppszFileName
, pmt
);
636 /* copy file name & media type if available, otherwise clear the outputs */
637 if (This
->pszFileName
)
639 *ppszFileName
= CoTaskMemAlloc((strlenW(This
->pszFileName
) + 1) * sizeof(WCHAR
));
640 strcpyW(*ppszFileName
, This
->pszFileName
);
643 *ppszFileName
= NULL
;
647 CopyMediaType(pmt
, This
->pmt
);
650 ZeroMemory(pmt
, sizeof(*pmt
));
655 static const IFileSourceFilterVtbl FileSource_Vtbl
=
657 FileSource_QueryInterface
,
661 FileSource_GetCurFile
665 /* the dwUserData passed back to user */
666 typedef struct DATAREQUEST
668 IMediaSample
* pSample
; /* sample passed to us by user */
669 DWORD_PTR dwUserData
; /* user data passed to us */
670 OVERLAPPED ovl
; /* our overlapped structure */
672 struct DATAREQUEST
* pNext
; /* next data request in list */
675 void queue(DATAREQUEST
* pHead
, DATAREQUEST
* pItem
)
677 DATAREQUEST
* pCurrent
;
678 for (pCurrent
= pHead
; pCurrent
->pNext
; pCurrent
= pCurrent
->pNext
)
680 pCurrent
->pNext
= pItem
;
683 typedef struct FileAsyncReader
686 const struct IAsyncReaderVtbl
* lpVtblAR
;
691 DATAREQUEST
* pHead
; /* head of data request list */
692 CRITICAL_SECTION csList
; /* critical section to protect operations on list */
695 static HRESULT
AcceptProcAFR(LPVOID iface
, const AM_MEDIA_TYPE
*pmt
)
697 AsyncReader
*This
= (AsyncReader
*)iface
;
699 FIXME("(%p, %p)\n", iface
, pmt
);
701 if (IsEqualGUID(&pmt
->majortype
, &This
->pmt
->majortype
) &&
702 IsEqualGUID(&pmt
->subtype
, &This
->pmt
->subtype
) &&
703 IsEqualGUID(&pmt
->formattype
, &FORMAT_None
))
709 /* overriden pin functions */
711 static HRESULT WINAPI
FileAsyncReaderPin_QueryInterface(IPin
* iface
, REFIID riid
, LPVOID
* ppv
)
713 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
714 TRACE("(%s, %p)\n", qzdebugstr_guid(riid
), ppv
);
718 if (IsEqualIID(riid
, &IID_IUnknown
))
720 else if (IsEqualIID(riid
, &IID_IPin
))
722 else if (IsEqualIID(riid
, &IID_IAsyncReader
))
723 *ppv
= (LPVOID
)&This
->lpVtblAR
;
727 IUnknown_AddRef((IUnknown
*)(*ppv
));
731 FIXME("No interface for %s!\n", qzdebugstr_guid(riid
));
733 return E_NOINTERFACE
;
736 static ULONG WINAPI
FileAsyncReaderPin_Release(IPin
* iface
)
738 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
742 if (!InterlockedDecrement(&This
->pin
.pin
.refCount
))
744 DATAREQUEST
* pCurrent
;
746 for (pCurrent
= This
->pHead
; pCurrent
; pCurrent
= pNext
)
748 pNext
= pCurrent
->pNext
;
749 CoTaskMemFree(pCurrent
);
751 CloseHandle(This
->hFile
);
752 CloseHandle(This
->hEvent
);
756 return This
->pin
.pin
.refCount
;
759 static HRESULT WINAPI
FileAsyncReaderPin_EnumMediaTypes(IPin
* iface
, IEnumMediaTypes
** ppEnum
)
761 ENUMMEDIADETAILS emd
;
762 FileAsyncReader
*This
= (FileAsyncReader
*)iface
;
764 TRACE("(%p)\n", ppEnum
);
767 emd
.pMediaTypes
= ((AsyncReader
*)This
->pin
.pin
.pinInfo
.pFilter
)->pmt
;
769 return IEnumMediaTypesImpl_Construct(&emd
, ppEnum
);
772 static const IPinVtbl FileAsyncReaderPin_Vtbl
=
774 FileAsyncReaderPin_QueryInterface
,
776 FileAsyncReaderPin_Release
,
778 OutputPin_ReceiveConnection
,
780 IPinImpl_ConnectedTo
,
781 IPinImpl_ConnectionMediaType
,
782 IPinImpl_QueryPinInfo
,
783 IPinImpl_QueryDirection
,
785 IPinImpl_QueryAccept
,
786 FileAsyncReaderPin_EnumMediaTypes
,
787 IPinImpl_QueryInternalConnections
,
788 OutputPin_EndOfStream
,
789 OutputPin_BeginFlush
,
794 /* Function called as a helper to IPin_Connect */
795 /* specific AM_MEDIA_TYPE - it cannot be NULL */
796 /* this differs from standard OutputPin_ConnectSpecific only in that it
797 * doesn't need the IMemInputPin interface on the receiving pin */
798 static HRESULT
FileAsyncReaderPin_ConnectSpecific(IPin
* iface
, IPin
* pReceivePin
, const AM_MEDIA_TYPE
* pmt
)
800 OutputPin
*This
= (OutputPin
*)iface
;
803 TRACE("(%p, %p)\n", pReceivePin
, pmt
);
804 dump_AM_MEDIA_TYPE(pmt
);
806 /* FIXME: call queryacceptproc */
808 This
->pin
.pConnectedTo
= pReceivePin
;
809 IPin_AddRef(pReceivePin
);
810 CopyMediaType(&This
->pin
.mtCurrent
, pmt
);
812 hr
= IPin_ReceiveConnection(pReceivePin
, iface
, pmt
);
816 IPin_Release(This
->pin
.pConnectedTo
);
817 This
->pin
.pConnectedTo
= NULL
;
818 DeleteMediaType(&This
->pin
.mtCurrent
);
821 TRACE(" -- %lx\n", hr
);
825 static HRESULT
FileAsyncReader_Construct(HANDLE hFile
, IBaseFilter
* pBaseFilter
, LPCRITICAL_SECTION pCritSec
, IPin
** ppPin
)
827 FileAsyncReader
* pPinImpl
;
832 pPinImpl
= CoTaskMemAlloc(sizeof(*pPinImpl
));
835 return E_OUTOFMEMORY
;
837 piOutput
.dir
= PINDIR_OUTPUT
;
838 piOutput
.pFilter
= pBaseFilter
;
839 strcpyW(piOutput
.achName
, wszOutputPinName
);
841 if (SUCCEEDED(OutputPin_Init(&piOutput
, NULL
, pBaseFilter
, AcceptProcAFR
, pCritSec
, &pPinImpl
->pin
)))
843 pPinImpl
->pin
.pin
.lpVtbl
= &FileAsyncReaderPin_Vtbl
;
844 pPinImpl
->lpVtblAR
= &FileAsyncReader_Vtbl
;
845 pPinImpl
->hFile
= hFile
;
846 pPinImpl
->hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
847 pPinImpl
->bFlushing
= FALSE
;
848 pPinImpl
->pHead
= NULL
;
849 pPinImpl
->pin
.pConnectSpecific
= FileAsyncReaderPin_ConnectSpecific
;
850 InitializeCriticalSection(&pPinImpl
->csList
);
852 *ppPin
= (IPin
*)(&pPinImpl
->pin
.pin
.lpVtbl
);
860 static HRESULT WINAPI
FileAsyncReader_QueryInterface(IAsyncReader
* iface
, REFIID riid
, LPVOID
* ppv
)
862 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
864 return IPin_QueryInterface((IPin
*)This
, riid
, ppv
);
867 static ULONG WINAPI
FileAsyncReader_AddRef(IAsyncReader
* iface
)
869 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
871 return IPin_AddRef((IPin
*)This
);
874 static ULONG WINAPI
FileAsyncReader_Release(IAsyncReader
* iface
)
876 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
878 return IPin_Release((IPin
*)This
);
881 #define DEF_ALIGNMENT 1
883 static HRESULT WINAPI
FileAsyncReader_RequestAllocator(IAsyncReader
* iface
, IMemAllocator
* pPreferred
, ALLOCATOR_PROPERTIES
* pProps
, IMemAllocator
** ppActual
)
887 TRACE("(%p, %p, %p)\n", pPreferred
, pProps
, ppActual
);
889 if (!pProps
->cbAlign
|| (pProps
->cbAlign
% DEF_ALIGNMENT
) != 0)
890 pProps
->cbAlign
= DEF_ALIGNMENT
;
894 ALLOCATOR_PROPERTIES PropsActual
;
895 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, &PropsActual
);
896 /* FIXME: check we are still aligned */
899 IMemAllocator_AddRef(pPreferred
);
900 *ppActual
= pPreferred
;
901 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr
);
908 hr
= CoCreateInstance(&CLSID_MemoryAllocator
, NULL
, CLSCTX_INPROC
, &IID_IMemAllocator
, (LPVOID
*)&pPreferred
);
912 ALLOCATOR_PROPERTIES PropsActual
;
913 hr
= IMemAllocator_SetProperties(pPreferred
, pProps
, &PropsActual
);
914 /* FIXME: check we are still aligned */
917 IMemAllocator_AddRef(pPreferred
);
918 *ppActual
= pPreferred
;
919 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr
);
928 IMemAllocator_Release(pPreferred
);
931 TRACE("-- %lx\n", hr
);
935 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
936 * however, this would be quite complicated to do and may be a bit error prone */
937 static HRESULT WINAPI
FileAsyncReader_Request(IAsyncReader
* iface
, IMediaSample
* pSample
, DWORD_PTR dwUser
)
939 REFERENCE_TIME Start
;
941 DATAREQUEST
* pDataRq
;
944 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
946 TRACE("(%p, %lx)\n", pSample
, dwUser
);
948 /* check flushing state */
950 return VFW_E_WRONG_STATE
;
952 if (!(pDataRq
= CoTaskMemAlloc(sizeof(*pDataRq
))))
955 /* get start and stop positions in bytes */
957 hr
= IMediaSample_GetTime(pSample
, &Start
, &Stop
);
960 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
964 DWORD dwLength
= (DWORD
) BYTES_FROM_MEDIATIME(Stop
- Start
);
966 pDataRq
->ovl
.Offset
= (DWORD
) BYTES_FROM_MEDIATIME(Start
);
967 pDataRq
->ovl
.OffsetHigh
= (DWORD
)(BYTES_FROM_MEDIATIME(Start
) >> (sizeof(DWORD
) * 8));
968 pDataRq
->ovl
.hEvent
= This
->hEvent
;
969 pDataRq
->dwUserData
= dwUser
;
970 pDataRq
->pNext
= NULL
;
971 /* we violate traditional COM rules here by maintaining
972 * a reference to the sample, but not calling AddRef, but
973 * that's what MSDN says to do */
974 pDataRq
->pSample
= pSample
;
976 EnterCriticalSection(&This
->csList
);
979 /* adds data request to end of list */
980 queue(This
->pHead
, pDataRq
);
982 This
->pHead
= pDataRq
;
984 LeaveCriticalSection(&This
->csList
);
986 /* this is definitely not how it is implemented on Win9x
987 * as they do not support async reads on files, but it is
988 * sooo much easier to use this than messing around with threads!
990 if (!ReadFile(This
->hFile
, pBuffer
, dwLength
, NULL
, &pDataRq
->ovl
))
991 hr
= HRESULT_FROM_WIN32(GetLastError());
993 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
994 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
998 if (FAILED(hr
) && pDataRq
)
1000 EnterCriticalSection(&This
->csList
);
1002 DATAREQUEST
* pCurrent
;
1003 for (pCurrent
= This
->pHead
; pCurrent
&& pCurrent
->pNext
; pCurrent
= pCurrent
->pNext
)
1004 if (pCurrent
->pNext
== pDataRq
)
1006 pCurrent
->pNext
= pDataRq
->pNext
;
1010 LeaveCriticalSection(&This
->csList
);
1011 CoTaskMemFree(pDataRq
);
1014 TRACE("-- %lx\n", hr
);
1018 static HRESULT WINAPI
FileAsyncReader_WaitForNext(IAsyncReader
* iface
, DWORD dwTimeout
, IMediaSample
** ppSample
, DWORD_PTR
* pdwUser
)
1021 DATAREQUEST
* pDataRq
= NULL
;
1022 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1024 TRACE("(%lu, %p, %p)\n", dwTimeout
, ppSample
, pdwUser
);
1026 /* FIXME: we could do with improving this by waiting for an array of event handles
1027 * and then determining which one finished and removing that from the list, otherwise
1028 * we will end up waiting for longer than we should do, if a later request finishes
1029 * before an earlier one */
1034 /* we return immediately if flushing */
1035 if (This
->bFlushing
)
1036 hr
= VFW_E_WRONG_STATE
;
1040 /* wait for the read to finish or timeout */
1041 if (WaitForSingleObject(This
->hEvent
, dwTimeout
) == WAIT_TIMEOUT
)
1046 EnterCriticalSection(&This
->csList
);
1048 pDataRq
= This
->pHead
;
1049 if (pDataRq
== NULL
)
1052 This
->pHead
= pDataRq
->pNext
;
1054 LeaveCriticalSection(&This
->csList
);
1060 /* get any errors */
1061 if (!GetOverlappedResult(This
->hFile
, &pDataRq
->ovl
, &dwBytes
, FALSE
))
1062 hr
= HRESULT_FROM_WIN32(GetLastError());
1067 *ppSample
= pDataRq
->pSample
;
1068 *pdwUser
= pDataRq
->dwUserData
;
1074 /* no need to close event handle since we will close it when the pin is destroyed */
1075 CoTaskMemFree(pDataRq
);
1078 TRACE("-- %lx\n", hr
);
1082 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
);
1084 static HRESULT WINAPI
FileAsyncReader_SyncReadAligned(IAsyncReader
* iface
, IMediaSample
* pSample
)
1087 REFERENCE_TIME tStart
;
1088 REFERENCE_TIME tStop
;
1091 TRACE("(%p)\n", pSample
);
1093 hr
= IMediaSample_GetTime(pSample
, &tStart
, &tStop
);
1096 hr
= IMediaSample_GetPointer(pSample
, &pBuffer
);
1099 hr
= FileAsyncReader_SyncRead(iface
,
1100 BYTES_FROM_MEDIATIME(tStart
),
1101 (LONG
) BYTES_FROM_MEDIATIME(tStop
- tStart
),
1104 TRACE("-- %lx\n", hr
);
1108 static HRESULT WINAPI
FileAsyncReader_SyncRead(IAsyncReader
* iface
, LONGLONG llPosition
, LONG lLength
, BYTE
* pBuffer
)
1112 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1114 TRACE("(%lx%08lx, %ld, %p)\n", (ULONG
)(llPosition
>> 32), (ULONG
)llPosition
, lLength
, pBuffer
);
1116 ZeroMemory(&ovl
, sizeof(ovl
));
1118 ovl
.hEvent
= CreateEventW(NULL
, 0, 0, NULL
);
1119 /* NOTE: llPosition is the actual byte position to start reading from */
1120 ovl
.Offset
= (DWORD
) llPosition
;
1121 ovl
.OffsetHigh
= (DWORD
) (llPosition
>> (sizeof(DWORD
) * 8));
1123 if (!ReadFile(This
->hFile
, pBuffer
, lLength
, NULL
, &ovl
))
1124 hr
= HRESULT_FROM_WIN32(GetLastError());
1126 if (hr
== HRESULT_FROM_WIN32(ERROR_IO_PENDING
))
1133 if (!GetOverlappedResult(This
->hFile
, &ovl
, &dwBytesRead
, TRUE
))
1134 hr
= HRESULT_FROM_WIN32(GetLastError());
1137 CloseHandle(ovl
.hEvent
);
1139 TRACE("-- %lx\n", hr
);
1143 static HRESULT WINAPI
FileAsyncReader_Length(IAsyncReader
* iface
, LONGLONG
* pTotal
, LONGLONG
* pAvailable
)
1147 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1149 TRACE("(%p, %p)\n", pTotal
, pAvailable
);
1151 if (((dwSizeLow
= GetFileSize(This
->hFile
, &dwSizeHigh
)) == -1) &&
1152 (GetLastError() != NO_ERROR
))
1153 return HRESULT_FROM_WIN32(GetLastError());
1155 *pTotal
= (LONGLONG
)dwSizeLow
| (LONGLONG
)dwSizeHigh
<< (sizeof(DWORD
) * 8);
1157 *pAvailable
= *pTotal
;
1162 static HRESULT WINAPI
FileAsyncReader_BeginFlush(IAsyncReader
* iface
)
1164 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1168 This
->bFlushing
= TRUE
;
1169 CancelIo(This
->hFile
);
1170 SetEvent(This
->hEvent
);
1172 /* FIXME: free list */
1177 static HRESULT WINAPI
FileAsyncReader_EndFlush(IAsyncReader
* iface
)
1179 ICOM_THIS_From_IAsyncReader(FileAsyncReader
, iface
);
1183 This
->bFlushing
= FALSE
;
1188 static const IAsyncReaderVtbl FileAsyncReader_Vtbl
=
1190 FileAsyncReader_QueryInterface
,
1191 FileAsyncReader_AddRef
,
1192 FileAsyncReader_Release
,
1193 FileAsyncReader_RequestAllocator
,
1194 FileAsyncReader_Request
,
1195 FileAsyncReader_WaitForNext
,
1196 FileAsyncReader_SyncReadAligned
,
1197 FileAsyncReader_SyncRead
,
1198 FileAsyncReader_Length
,
1199 FileAsyncReader_BeginFlush
,
1200 FileAsyncReader_EndFlush
,