WM_PAINT(wParam) might be a valid HDC.
[wine/wine64.git] / dlls / quartz / filesource.c
blob3ff17040e0f9df4551b2af2dabd80606bdd204e3
1 /*
2 * File Source Filter
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"
25 #include "pin.h"
26 #include "uuids.h"
27 #include "vfwmsgs.h"
28 #include "winbase.h"
29 #include "winreg.h"
30 #include <assert.h>
32 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
34 static const WCHAR wszOutputPinName[] = { 'O','u','t','p','u','t',0 };
36 /* see IAsyncReader::Request on MSDN for the explanation of this */
37 #define BYTES_FROM_MEDIATIME(time) ((time) / 10000000)
39 typedef struct AsyncReader
41 const struct IBaseFilterVtbl * lpVtbl;
42 const struct IFileSourceFilterVtbl * lpVtblFSF;
44 ULONG refCount;
45 FILTER_INFO filterInfo;
46 FILTER_STATE state;
47 CRITICAL_SECTION csFilter;
49 IPin * pOutputPin;
50 LPOLESTR pszFileName;
51 AM_MEDIA_TYPE * pmt;
52 } AsyncReader;
54 static const struct IBaseFilterVtbl AsyncReader_Vtbl;
55 static const struct IFileSourceFilterVtbl FileSource_Vtbl;
56 static const struct IAsyncReaderVtbl FileAsyncReader_Vtbl;
58 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
60 #define _IFileSourceFilter_Offset ((int)(&(((AsyncReader*)0)->lpVtblFSF)))
61 #define ICOM_THIS_From_IFileSourceFilter(impl, iface) impl* This = (impl*)(((char*)iface)-_IFileSourceFilter_Offset);
63 #define _IAsyncReader_Offset ((int)(&(((FileAsyncReader*)0)->lpVtblAR)))
64 #define ICOM_THIS_From_IAsyncReader(impl, iface) impl* This = (impl*)(((char*)iface)-_IAsyncReader_Offset);
66 static HRESULT process_extensions(HKEY hkeyExtensions, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
68 /* FIXME: implement */
69 return E_NOTIMPL;
72 static unsigned char byte_from_hex_char(WCHAR wHex)
74 switch (tolowerW(wHex))
76 case '0':
77 case '1':
78 case '2':
79 case '3':
80 case '4':
81 case '5':
82 case '6':
83 case '7':
84 case '8':
85 case '9':
86 return wHex - '0';
87 case 'a':
88 case 'b':
89 case 'c':
90 case 'd':
91 case 'e':
92 case 'f':
93 return wHex - 'a' + 10;
94 default:
95 return 0;
99 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
101 ULONG ulOffset;
102 ULONG ulBytes;
103 BYTE * pbMask;
104 BYTE * pbValue;
105 BYTE * pbFile;
106 HRESULT hr = S_OK;
107 ULONG strpos;
109 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
111 /* format: "offset, bytestocompare, mask, value" */
113 ulOffset = strtolW(wszPatternString, NULL, 10);
115 if (!(wszPatternString = strchrW(wszPatternString, ',')))
116 return E_INVALIDARG;
118 wszPatternString++; /* skip ',' */
120 ulBytes = strtolW(wszPatternString, NULL, 10);
122 pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
123 pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
124 pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
126 /* default mask is match everything */
127 memset(pbMask, 0xFF, ulBytes);
129 if (!(wszPatternString = strchrW(wszPatternString, ',')))
130 hr = E_INVALIDARG;
132 wszPatternString++; /* skip ',' */
134 if (hr == S_OK)
136 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
139 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
141 if ((strpos % 2) == 1) /* odd numbered position */
142 pbMask[strpos / 2] |= byte_from_hex_char(*wszPatternString);
143 else
144 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
147 if (!(wszPatternString = strchrW(wszPatternString, ',')))
148 hr = E_INVALIDARG;
150 wszPatternString++; /* skip ',' */
153 if (hr == S_OK)
155 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
158 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
160 if ((strpos % 2) == 1) /* odd numbered position */
161 pbValue[strpos / 2] |= byte_from_hex_char(*wszPatternString);
162 else
163 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
167 if (hr == S_OK)
168 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
170 if (hr == S_OK)
172 ULONG i;
173 for (i = 0; i < ulBytes; i++)
174 if ((pbFile[i] & pbMask[i]) != pbValue[i])
176 hr = S_FALSE;
177 break;
181 HeapFree(GetProcessHeap(), 0, pbMask);
182 HeapFree(GetProcessHeap(), 0, pbValue);
183 HeapFree(GetProcessHeap(), 0, pbFile);
185 /* if we encountered no errors with this string, and there is a following tuple, then we
186 * have to match that as well to succeed */
187 if ((hr == S_OK) && (wszPatternString = strchrW(wszPatternString, ',')))
188 return process_pattern_string(wszPatternString + 1, pReader);
189 else
190 return hr;
193 static HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
195 HKEY hkeyMediaType = NULL;
196 HRESULT hr = S_OK;
197 BOOL bFound = FALSE;
198 WCHAR wszMediaType[] = {'M','e','d','i','a',' ','T','y','p','e',0};
200 CopyMemory(majorType, &GUID_NULL, sizeof(*majorType));
201 CopyMemory(minorType, &GUID_NULL, sizeof(*minorType));
203 hr = HRESULT_FROM_WIN32(RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType));
205 if (SUCCEEDED(hr))
207 DWORD indexMajor;
209 for (indexMajor = 0; !bFound; indexMajor++)
211 HKEY hkeyMajor;
212 WCHAR wszMajorKeyName[CHARS_IN_GUID];
213 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
214 const WCHAR wszExtensions[] = {'E','x','t','e','n','s','i','o','n','s',0};
216 if (RegEnumKeyExW(hkeyMediaType, indexMajor, wszMajorKeyName, &dwKeyNameLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
217 break;
218 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
219 break;
220 TRACE("%s\n", debugstr_w(wszMajorKeyName));
221 if (!strcmpW(wszExtensions, wszMajorKeyName))
223 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType) == S_OK)
224 bFound = TRUE;
226 else
228 DWORD indexMinor;
230 for (indexMinor = 0; !bFound; indexMinor++)
232 HKEY hkeyMinor;
233 WCHAR wszMinorKeyName[CHARS_IN_GUID];
234 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
235 DWORD maxValueLen;
236 DWORD indexValue;
238 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
239 break;
241 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
242 break;
244 TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
246 if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
247 break;
249 for (indexValue = 0; !bFound; indexValue++)
251 DWORD dwType;
252 WCHAR wszValueName[14]; /* longest name we should encounter will be "Source Filter" */
253 LPWSTR wszPatternString = HeapAlloc(GetProcessHeap(), 0, maxValueLen);
254 DWORD dwValueNameLen = sizeof(wszValueName) / sizeof(wszValueName[0]); /* remember this is in chars */
255 DWORD dwDataLen = maxValueLen; /* remember this is in bytes */
256 const WCHAR wszSourceFilter[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
257 LONG temp;
259 if ((temp = RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen)) != ERROR_SUCCESS)
261 HeapFree(GetProcessHeap(), 0, wszPatternString);
262 break;
265 /* if it is not the source filter value */
266 if (strcmpW(wszValueName, wszSourceFilter))
268 if (process_pattern_string(wszPatternString, pReader) == S_OK)
270 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName, majorType)) &&
271 SUCCEEDED(CLSIDFromString(wszMinorKeyName, minorType)))
272 bFound = TRUE;
275 HeapFree(GetProcessHeap(), 0, wszPatternString);
277 CloseHandle(hkeyMinor);
280 CloseHandle(hkeyMajor);
283 CloseHandle(hkeyMediaType);
285 if (SUCCEEDED(hr) && !bFound)
287 ERR("Media class not found\n");
288 hr = S_FALSE;
290 else if (bFound)
291 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType), qzdebugstr_guid(minorType));
293 return hr;
296 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
298 AsyncReader * pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
300 if (!pAsyncRead)
301 return E_OUTOFMEMORY;
303 pAsyncRead->lpVtbl = &AsyncReader_Vtbl;
304 pAsyncRead->lpVtblFSF = &FileSource_Vtbl;
305 pAsyncRead->refCount = 1;
306 pAsyncRead->filterInfo.achName[0] = '\0';
307 pAsyncRead->filterInfo.pGraph = NULL;
308 pAsyncRead->pOutputPin = NULL;
310 InitializeCriticalSection(&pAsyncRead->csFilter);
312 pAsyncRead->pszFileName = NULL;
313 pAsyncRead->pmt = NULL;
315 *ppv = (LPVOID)pAsyncRead;
317 TRACE("-- created at %p\n", pAsyncRead);
319 return S_OK;
322 /** IUnkown methods **/
324 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
326 ICOM_THIS(AsyncReader, iface);
328 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
330 *ppv = NULL;
332 if (IsEqualIID(riid, &IID_IUnknown))
333 *ppv = (LPVOID)This;
334 else if (IsEqualIID(riid, &IID_IPersist))
335 *ppv = (LPVOID)This;
336 else if (IsEqualIID(riid, &IID_IMediaFilter))
337 *ppv = (LPVOID)This;
338 else if (IsEqualIID(riid, &IID_IBaseFilter))
339 *ppv = (LPVOID)This;
340 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
341 *ppv = (LPVOID)(&This->lpVtblFSF);
343 if (*ppv)
345 IUnknown_AddRef((IUnknown *)(*ppv));
346 return S_OK;
349 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
351 return E_NOINTERFACE;
354 static ULONG WINAPI AsyncReader_AddRef(IBaseFilter * iface)
356 ICOM_THIS(AsyncReader, iface);
358 TRACE("()\n");
360 return InterlockedIncrement(&This->refCount);
363 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
365 ICOM_THIS(AsyncReader, iface);
367 TRACE("()\n");
369 if (!InterlockedDecrement(&This->refCount))
371 if (This->pOutputPin)
372 IPin_Release(This->pOutputPin);
373 DeleteCriticalSection(&This->csFilter);
374 This->lpVtbl = NULL;
375 CoTaskMemFree(This);
376 return 0;
378 else
379 return This->refCount;
382 /** IPersist methods **/
384 static HRESULT WINAPI AsyncReader_GetClassID(IBaseFilter * iface, CLSID * pClsid)
386 TRACE("(%p)\n", pClsid);
388 *pClsid = CLSID_AsyncReader;
390 return S_OK;
393 /** IMediaFilter methods **/
395 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
397 ICOM_THIS(AsyncReader, iface);
399 TRACE("()\n");
401 This->state = State_Stopped;
403 return S_OK;
406 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
408 ICOM_THIS(AsyncReader, iface);
410 TRACE("()\n");
412 This->state = State_Paused;
414 return S_OK;
417 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
419 ICOM_THIS(AsyncReader, iface);
421 TRACE("(%lx%08lx)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
423 This->state = State_Running;
425 return S_OK;
428 static HRESULT WINAPI AsyncReader_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
430 ICOM_THIS(AsyncReader, iface);
432 TRACE("(%lu, %p)\n", dwMilliSecsTimeout, pState);
434 *pState = This->state;
436 return S_OK;
439 static HRESULT WINAPI AsyncReader_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
441 /* ICOM_THIS(AsyncReader, iface);*/
443 TRACE("(%p)\n", pClock);
445 return S_OK;
448 static HRESULT WINAPI AsyncReader_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
450 /* ICOM_THIS(AsyncReader, iface);*/
452 TRACE("(%p)\n", ppClock);
454 return S_OK;
457 /** IBaseFilter methods **/
459 static HRESULT WINAPI AsyncReader_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
461 ENUMPINDETAILS epd;
462 ICOM_THIS(AsyncReader, iface);
464 TRACE("(%p)\n", ppEnum);
466 epd.cPins = This->pOutputPin ? 1 : 0;
467 epd.ppPins = &This->pOutputPin;
468 return IEnumPinsImpl_Construct(&epd, ppEnum);
471 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
473 FIXME("(%s, %p)\n", debugstr_w(Id), ppPin);
475 return E_NOTIMPL;
478 static HRESULT WINAPI AsyncReader_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
480 ICOM_THIS(AsyncReader, iface);
482 TRACE("(%p)\n", pInfo);
484 strcpyW(pInfo->achName, This->filterInfo.achName);
485 pInfo->pGraph = This->filterInfo.pGraph;
487 if (pInfo->pGraph)
488 IFilterGraph_AddRef(pInfo->pGraph);
490 return S_OK;
493 static HRESULT WINAPI AsyncReader_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
495 ICOM_THIS(AsyncReader, iface);
497 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
499 if (pName)
500 strcpyW(This->filterInfo.achName, pName);
501 else
502 *This->filterInfo.achName = 0;
503 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
505 return S_OK;
508 static HRESULT WINAPI AsyncReader_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
510 FIXME("(%p)\n", pVendorInfo);
512 return E_NOTIMPL;
515 static const IBaseFilterVtbl AsyncReader_Vtbl =
517 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
518 AsyncReader_QueryInterface,
519 AsyncReader_AddRef,
520 AsyncReader_Release,
521 AsyncReader_GetClassID,
522 AsyncReader_Stop,
523 AsyncReader_Pause,
524 AsyncReader_Run,
525 AsyncReader_GetState,
526 AsyncReader_SetSyncSource,
527 AsyncReader_GetSyncSource,
528 AsyncReader_EnumPins,
529 AsyncReader_FindPin,
530 AsyncReader_QueryFilterInfo,
531 AsyncReader_JoinFilterGraph,
532 AsyncReader_QueryVendorInfo
535 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
537 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
539 return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->lpVtbl, riid, ppv);
542 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
544 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
546 return IBaseFilter_AddRef((IFileSourceFilter*)&This->lpVtbl);
549 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
551 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
553 return IBaseFilter_Release((IFileSourceFilter*)&This->lpVtbl);
556 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
558 HRESULT hr;
559 HANDLE hFile;
560 IAsyncReader * pReader = NULL;
561 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
563 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
565 /* open file */
566 /* FIXME: check the sharing values that native uses */
567 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
569 if (hFile == INVALID_HANDLE_VALUE)
571 return HRESULT_FROM_WIN32(GetLastError());
574 /* create pin */
575 hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->lpVtbl, &This->csFilter, &This->pOutputPin);
577 if (SUCCEEDED(hr))
578 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
580 /* store file name & media type */
581 if (SUCCEEDED(hr))
583 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
584 strcpyW(This->pszFileName, pszFileName);
585 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
586 if (!pmt)
588 This->pmt->bFixedSizeSamples = TRUE;
589 This->pmt->bTemporalCompression = FALSE;
590 This->pmt->cbFormat = 0;
591 This->pmt->pbFormat = NULL;
592 This->pmt->pUnk = NULL;
593 This->pmt->lSampleSize = 0;
594 memcpy(&This->pmt->formattype, &FORMAT_None, sizeof(FORMAT_None));
595 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype);
596 if (FAILED(hr))
598 CoTaskMemFree(This->pmt);
599 This->pmt = NULL;
602 else
603 CopyMediaType(This->pmt, pmt);
606 if (pReader)
607 IAsyncReader_Release(pReader);
609 if (FAILED(hr))
611 if (This->pOutputPin)
613 IPin_Release(This->pOutputPin);
614 This->pOutputPin = NULL;
616 if (This->pszFileName)
618 CoTaskMemFree(This->pszFileName);
619 This->pszFileName = NULL;
621 CloseHandle(hFile);
624 /* FIXME: check return codes */
625 return hr;
628 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
630 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
632 TRACE("(%p, %p)\n", ppszFileName, pmt);
634 /* copy file name & media type if available, otherwise clear the outputs */
635 if (This->pszFileName)
637 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
638 strcpyW(*ppszFileName, This->pszFileName);
640 else
641 *ppszFileName = NULL;
643 if (This->pmt)
645 CopyMediaType(pmt, This->pmt);
647 else
648 ZeroMemory(pmt, sizeof(*pmt));
650 return S_OK;
653 static const IFileSourceFilterVtbl FileSource_Vtbl =
655 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
656 FileSource_QueryInterface,
657 FileSource_AddRef,
658 FileSource_Release,
659 FileSource_Load,
660 FileSource_GetCurFile
664 /* the dwUserData passed back to user */
665 typedef struct DATAREQUEST
667 IMediaSample * pSample; /* sample passed to us by user */
668 DWORD_PTR dwUserData; /* user data passed to us */
669 OVERLAPPED ovl; /* our overlapped structure */
671 struct DATAREQUEST * pNext; /* next data request in list */
672 } DATAREQUEST;
674 void queue(DATAREQUEST * pHead, DATAREQUEST * pItem)
676 DATAREQUEST * pCurrent;
677 for (pCurrent = pHead; pCurrent->pNext; pCurrent = pCurrent->pNext)
679 pCurrent->pNext = pItem;
682 typedef struct FileAsyncReader
684 OutputPin pin;
685 const struct IAsyncReaderVtbl * lpVtblAR;
687 HANDLE hFile;
688 HANDLE hEvent;
689 BOOL bFlushing;
690 DATAREQUEST * pHead; /* head of data request list */
691 CRITICAL_SECTION csList; /* critical section to protect operations on list */
692 } FileAsyncReader;
694 static HRESULT AcceptProcAFR(LPVOID iface, const AM_MEDIA_TYPE *pmt)
696 ICOM_THIS(AsyncReader, iface);
698 FIXME("(%p, %p)\n", iface, pmt);
700 if (IsEqualGUID(&pmt->majortype, &This->pmt->majortype) &&
701 IsEqualGUID(&pmt->subtype, &This->pmt->subtype) &&
702 IsEqualGUID(&pmt->formattype, &FORMAT_None))
703 return S_OK;
705 return S_FALSE;
708 /* overriden pin functions */
710 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
712 ICOM_THIS(FileAsyncReader, iface);
713 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
715 *ppv = NULL;
717 if (IsEqualIID(riid, &IID_IUnknown))
718 *ppv = (LPVOID)This;
719 else if (IsEqualIID(riid, &IID_IPin))
720 *ppv = (LPVOID)This;
721 else if (IsEqualIID(riid, &IID_IAsyncReader))
722 *ppv = (LPVOID)&This->lpVtblAR;
724 if (*ppv)
726 IUnknown_AddRef((IUnknown *)(*ppv));
727 return S_OK;
730 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
732 return E_NOINTERFACE;
735 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
737 ICOM_THIS(FileAsyncReader, iface);
739 TRACE("()\n");
741 if (!InterlockedDecrement(&This->pin.pin.refCount))
743 DATAREQUEST * pCurrent;
744 DATAREQUEST * pNext;
745 for (pCurrent = This->pHead; pCurrent; pCurrent = pNext)
747 pNext = pCurrent->pNext;
748 CoTaskMemFree(pCurrent);
750 CloseHandle(This->hFile);
751 CloseHandle(This->hEvent);
752 CoTaskMemFree(This);
753 return 0;
755 return This->pin.pin.refCount;
758 static HRESULT WINAPI FileAsyncReaderPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
760 ENUMMEDIADETAILS emd;
761 ICOM_THIS(FileAsyncReader, iface);
763 TRACE("(%p)\n", ppEnum);
765 emd.cMediaTypes = 1;
766 emd.pMediaTypes = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
768 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
771 static HRESULT WINAPI FileAsyncReaderPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
773 HRESULT hr = S_OK;
774 ICOM_THIS(OutputPin, iface);
776 TRACE("(%p, %p)\n", pReceivePin, pmt);
777 dump_AM_MEDIA_TYPE(pmt);
779 /* If we try to connect to ourself, we will definitely deadlock.
780 * There are other cases where we could deadlock too, but this
781 * catches the obvious case */
782 assert(pReceivePin != iface);
784 EnterCriticalSection(This->pin.pCritSec);
786 /* if we have been a specific type to connect with, then we can either connect
787 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
788 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
789 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
790 else
792 /* negotiate media type */
794 IEnumMediaTypes * pEnumCandidates;
795 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
797 if (SUCCEEDED(IPin_EnumMediaTypes(iface, &pEnumCandidates)))
799 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
801 /* try this filter's media types first */
802 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
804 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
805 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
807 hr = S_OK;
808 CoTaskMemFree(pmtCandidate);
809 break;
811 CoTaskMemFree(pmtCandidate);
813 IEnumMediaTypes_Release(pEnumCandidates);
816 /* then try receiver filter's media types */
817 if (hr != S_OK && SUCCEEDED(IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
819 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
821 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
822 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
824 hr = S_OK;
825 CoTaskMemFree(pmtCandidate);
826 break;
828 CoTaskMemFree(pmtCandidate);
829 } /* while */
830 IEnumMediaTypes_Release(pEnumCandidates);
831 } /* if not found */
832 } /* if negotiate media type */
833 } /* if succeeded */
834 LeaveCriticalSection(This->pin.pCritSec);
836 TRACE("-- %lx\n", hr);
837 return hr;
840 static const IPinVtbl FileAsyncReaderPin_Vtbl =
842 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
843 FileAsyncReaderPin_QueryInterface,
844 IPinImpl_AddRef,
845 FileAsyncReaderPin_Release,
846 FileAsyncReaderPin_Connect,
847 OutputPin_ReceiveConnection,
848 IPinImpl_Disconnect,
849 IPinImpl_ConnectedTo,
850 IPinImpl_ConnectionMediaType,
851 IPinImpl_QueryPinInfo,
852 IPinImpl_QueryDirection,
853 IPinImpl_QueryId,
854 IPinImpl_QueryAccept,
855 FileAsyncReaderPin_EnumMediaTypes,
856 IPinImpl_QueryInternalConnections,
857 OutputPin_EndOfStream,
858 OutputPin_BeginFlush,
859 OutputPin_EndFlush,
860 OutputPin_NewSegment
863 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
865 FileAsyncReader * pPinImpl;
866 PIN_INFO piOutput;
868 *ppPin = NULL;
870 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
872 if (!pPinImpl)
873 return E_OUTOFMEMORY;
875 piOutput.dir = PINDIR_OUTPUT;
876 piOutput.pFilter = pBaseFilter;
877 strcpyW(piOutput.achName, wszOutputPinName);
879 if (SUCCEEDED(OutputPin_Init(&piOutput, pBaseFilter, AcceptProcAFR, pCritSec, &pPinImpl->pin)))
881 pPinImpl->pin.pin.lpVtbl = &FileAsyncReaderPin_Vtbl;
882 pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
883 pPinImpl->hFile = hFile;
884 pPinImpl->hEvent = CreateEventW(NULL, 0, 0, NULL);
885 pPinImpl->bFlushing = FALSE;
886 pPinImpl->pHead = NULL;
888 *ppPin = (IPin *)(&pPinImpl->pin.pin.lpVtbl);
889 return S_OK;
891 return E_FAIL;
894 /* IAsyncReader */
896 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
898 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
900 return IPin_QueryInterface((IPin *)This, riid, ppv);
903 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
905 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
907 return IPin_AddRef((IPin *)This);
910 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
912 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
914 return IPin_Release((IPin *)This);
917 #define DEF_ALIGNMENT 1
919 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
921 HRESULT hr = S_OK;
923 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
925 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
926 pProps->cbAlign = DEF_ALIGNMENT;
928 if (pPreferred)
930 ALLOCATOR_PROPERTIES PropsActual;
931 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
932 /* FIXME: check we are still aligned */
933 if (SUCCEEDED(hr))
935 IMemAllocator_AddRef(pPreferred);
936 *ppActual = pPreferred;
937 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr);
938 return S_OK;
942 pPreferred = NULL;
944 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
946 if (SUCCEEDED(hr))
948 ALLOCATOR_PROPERTIES PropsActual;
949 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
950 /* FIXME: check we are still aligned */
951 if (SUCCEEDED(hr))
953 IMemAllocator_AddRef(pPreferred);
954 *ppActual = pPreferred;
955 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr);
956 return S_OK;
960 if (FAILED(hr))
962 *ppActual = NULL;
963 if (pPreferred)
964 IMemAllocator_Release(pPreferred);
967 TRACE("-- %lx\n", hr);
968 return hr;
971 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
972 * however, this would be quite complicated to do and may be a bit error prone */
973 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
975 REFERENCE_TIME Start;
976 REFERENCE_TIME Stop;
977 DATAREQUEST * pDataRq;
978 BYTE * pBuffer;
979 HRESULT hr = S_OK;
980 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
982 TRACE("(%p, %lx)\n", pSample, dwUser);
984 /* check flushing state */
985 if (This->bFlushing)
986 return VFW_E_WRONG_STATE;
988 if (!(pDataRq = CoTaskMemAlloc(sizeof(*pDataRq))))
989 hr = E_OUTOFMEMORY;
991 /* get start and stop positions in bytes */
992 if (SUCCEEDED(hr))
993 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
995 if (SUCCEEDED(hr))
996 hr = IMediaSample_GetPointer(pSample, &pBuffer);
998 if (SUCCEEDED(hr))
1000 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
1002 pDataRq->ovl.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
1003 pDataRq->ovl.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
1004 pDataRq->ovl.hEvent = This->hEvent;
1005 pDataRq->dwUserData = dwUser;
1006 pDataRq->pNext = NULL;
1007 /* we violate traditional COM rules here by maintaining
1008 * a reference to the sample, but not calling AddRef, but
1009 * that's what MSDN says to do */
1010 pDataRq->pSample = pSample;
1012 EnterCriticalSection(&This->csList);
1014 if (This->pHead)
1015 /* adds data request to end of list */
1016 queue(This->pHead, pDataRq);
1017 else
1018 This->pHead = pDataRq;
1020 LeaveCriticalSection(&This->csList);
1022 /* this is definitely not how it is implemented on Win9x
1023 * as they do not support async reads on files, but it is
1024 * sooo much easier to use this than messing around with threads!
1026 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1027 hr = HRESULT_FROM_WIN32(GetLastError());
1029 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1030 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1031 hr = S_OK;
1034 if (FAILED(hr) && pDataRq)
1036 EnterCriticalSection(&This->csList);
1038 DATAREQUEST * pCurrent;
1039 for (pCurrent = This->pHead; pCurrent && pCurrent->pNext; pCurrent = pCurrent->pNext)
1040 if (pCurrent->pNext == pDataRq)
1042 pCurrent->pNext = pDataRq->pNext;
1043 break;
1046 LeaveCriticalSection(&This->csList);
1047 CoTaskMemFree(pDataRq);
1050 TRACE("-- %lx\n", hr);
1051 return hr;
1054 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1056 HRESULT hr = S_OK;
1057 DATAREQUEST * pDataRq = NULL;
1058 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1060 TRACE("(%lu, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1062 /* FIXME: we could do with improving this by waiting for an array of event handles
1063 * and then determining which one finished and removing that from the list, otherwise
1064 * we will end up waiting for longer than we should do, if a later request finishes
1065 * before an earlier one */
1067 *ppSample = NULL;
1068 *pdwUser = 0;
1070 /* we return immediately if flushing */
1071 if (This->bFlushing)
1072 hr = VFW_E_WRONG_STATE;
1074 if (SUCCEEDED(hr))
1076 /* wait for the read to finish or timeout */
1077 if (WaitForSingleObject(This->hEvent, dwTimeout) == WAIT_TIMEOUT)
1078 hr = VFW_E_TIMEOUT;
1080 if (SUCCEEDED(hr))
1082 EnterCriticalSection(&This->csList);
1084 pDataRq = This->pHead;
1085 if (pDataRq == NULL)
1086 hr = E_FAIL;
1087 else
1088 This->pHead = pDataRq->pNext;
1090 LeaveCriticalSection(&This->csList);
1093 if (SUCCEEDED(hr))
1095 DWORD dwBytes;
1096 /* get any errors */
1097 if (!GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, TRUE))
1098 hr = HRESULT_FROM_WIN32(GetLastError());
1101 if (SUCCEEDED(hr))
1103 *ppSample = pDataRq->pSample;
1104 *pdwUser = pDataRq->dwUserData;
1107 /* clean up */
1108 if (pDataRq)
1110 /* no need to close event handle since we will close it when the pin is destroyed */
1111 CoTaskMemFree(pDataRq);
1114 TRACE("-- %lx\n", hr);
1115 return hr;
1118 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1120 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1122 BYTE * pBuffer;
1123 REFERENCE_TIME tStart;
1124 REFERENCE_TIME tStop;
1125 HRESULT hr;
1127 TRACE("(%p)\n", pSample);
1129 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1131 if (SUCCEEDED(hr))
1132 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1134 if (SUCCEEDED(hr))
1135 hr = FileAsyncReader_SyncRead(iface,
1136 BYTES_FROM_MEDIATIME(tStart),
1137 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1138 pBuffer);
1140 TRACE("-- %lx\n", hr);
1141 return hr;
1144 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1146 OVERLAPPED ovl;
1147 HRESULT hr = S_OK;
1148 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1150 TRACE("(%lx%08lx, %ld, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1152 ZeroMemory(&ovl, sizeof(ovl));
1154 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1155 /* NOTE: llPosition is the actual byte position to start reading from */
1156 ovl.Offset = (DWORD) llPosition;
1157 ovl.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1159 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1160 hr = HRESULT_FROM_WIN32(GetLastError());
1162 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1163 hr = S_OK;
1165 if (SUCCEEDED(hr))
1167 DWORD dwBytesRead;
1169 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1170 hr = HRESULT_FROM_WIN32(GetLastError());
1173 CloseHandle(ovl.hEvent);
1175 TRACE("-- %lx\n", hr);
1176 return hr;
1179 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1181 DWORD dwSizeLow;
1182 DWORD dwSizeHigh;
1183 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1185 TRACE("(%p, %p)\n", pTotal, pAvailable);
1187 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1188 (GetLastError() != NO_ERROR))
1189 return HRESULT_FROM_WIN32(GetLastError());
1191 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1193 *pAvailable = *pTotal;
1195 return S_OK;
1198 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1200 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1202 TRACE("()\n");
1204 This->bFlushing = TRUE;
1205 CancelIo(This->hFile);
1206 SetEvent(This->hEvent);
1208 /* FIXME: free list */
1210 return S_OK;
1213 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1215 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1217 TRACE("()\n");
1219 This->bFlushing = FALSE;
1221 return S_OK;
1224 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1226 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1227 FileAsyncReader_QueryInterface,
1228 FileAsyncReader_AddRef,
1229 FileAsyncReader_Release,
1230 FileAsyncReader_RequestAllocator,
1231 FileAsyncReader_Request,
1232 FileAsyncReader_WaitForNext,
1233 FileAsyncReader_SyncReadAligned,
1234 FileAsyncReader_SyncRead,
1235 FileAsyncReader_Length,
1236 FileAsyncReader_BeginFlush,
1237 FileAsyncReader_EndFlush,