Correct errors with move to kernel time functions.
[wine/multimedia.git] / dlls / quartz / filesource.c
blobaeab7cc5f8a674aa69f35b76f5d2078b1ba70dc3
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 "ntstatus.h"
31 #include <assert.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;
42 ULONG refCount;
43 FILTER_INFO filterInfo;
44 FILTER_STATE state;
45 CRITICAL_SECTION csFilter;
47 IPin * pOutputPin;
48 LPOLESTR pszFileName;
49 AM_MEDIA_TYPE * pmt;
50 } AsyncReader;
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 */
67 return E_NOTIMPL;
70 static unsigned char byte_from_hex_char(WCHAR wHex)
72 switch (tolowerW(wHex))
74 case '0':
75 case '1':
76 case '2':
77 case '3':
78 case '4':
79 case '5':
80 case '6':
81 case '7':
82 case '8':
83 case '9':
84 return wHex - '0';
85 case 'a':
86 case 'b':
87 case 'c':
88 case 'd':
89 case 'e':
90 case 'f':
91 return wHex - 'a' + 10;
92 default:
93 return 0;
97 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
99 ULONG ulOffset;
100 ULONG ulBytes;
101 BYTE * pbMask;
102 BYTE * pbValue;
103 BYTE * pbFile;
104 HRESULT hr = S_OK;
105 ULONG strpos;
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, ',')))
114 return E_INVALIDARG;
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, ',')))
128 hr = E_INVALIDARG;
130 wszPatternString++; /* skip ',' */
132 if (hr == S_OK)
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);
141 else
142 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
145 if (!(wszPatternString = strchrW(wszPatternString, ',')))
146 hr = E_INVALIDARG;
148 wszPatternString++; /* skip ',' */
151 if (hr == S_OK)
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);
160 else
161 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
165 if (hr == S_OK)
166 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
168 if (hr == S_OK)
170 ULONG i;
171 for (i = 0; i < ulBytes; i++)
172 if ((pbFile[i] & pbMask[i]) != pbValue[i])
174 hr = S_FALSE;
175 break;
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);
187 else
188 return hr;
191 static HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
193 HKEY hkeyMediaType = NULL;
194 HRESULT hr = S_OK;
195 BOOL bFound = FALSE;
196 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));
203 if (SUCCEEDED(hr))
205 DWORD indexMajor;
207 for (indexMajor = 0; !bFound; indexMajor++)
209 HKEY hkeyMajor;
210 WCHAR wszMajorKeyName[CHARS_IN_GUID];
211 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
212 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)
215 break;
216 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
217 break;
218 TRACE("%s\n", debugstr_w(wszMajorKeyName));
219 if (!strcmpW(wszExtensions, wszMajorKeyName))
221 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType) == S_OK)
222 bFound = TRUE;
224 else
226 DWORD indexMinor;
228 for (indexMinor = 0; !bFound; indexMinor++)
230 HKEY hkeyMinor;
231 WCHAR wszMinorKeyName[CHARS_IN_GUID];
232 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
233 DWORD maxValueLen;
234 DWORD indexValue;
236 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
237 break;
239 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
240 break;
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)
245 break;
247 for (indexValue = 0; !bFound; indexValue++)
249 DWORD dwType;
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 const WCHAR wszSourceFilter[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
255 LONG temp;
257 if ((temp = RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen)) != ERROR_SUCCESS)
259 HeapFree(GetProcessHeap(), 0, wszPatternString);
260 break;
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)))
270 bFound = TRUE;
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");
286 hr = S_FALSE;
288 else if (bFound)
289 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType), qzdebugstr_guid(minorType));
291 return hr;
294 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
296 AsyncReader * pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
298 if (!pAsyncRead)
299 return E_OUTOFMEMORY;
301 pAsyncRead->lpVtbl = &AsyncReader_Vtbl;
302 pAsyncRead->lpVtblFSF = &FileSource_Vtbl;
303 pAsyncRead->refCount = 1;
304 pAsyncRead->filterInfo.achName[0] = '\0';
305 pAsyncRead->filterInfo.pGraph = NULL;
306 pAsyncRead->pOutputPin = NULL;
308 InitializeCriticalSection(&pAsyncRead->csFilter);
310 pAsyncRead->pszFileName = NULL;
311 pAsyncRead->pmt = NULL;
313 *ppv = (LPVOID)pAsyncRead;
315 TRACE("-- created at %p\n", pAsyncRead);
317 return S_OK;
320 /** IUnkown methods **/
322 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
324 ICOM_THIS(AsyncReader, iface);
326 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
328 *ppv = NULL;
330 if (IsEqualIID(riid, &IID_IUnknown))
331 *ppv = (LPVOID)This;
332 else if (IsEqualIID(riid, &IID_IPersist))
333 *ppv = (LPVOID)This;
334 else if (IsEqualIID(riid, &IID_IMediaFilter))
335 *ppv = (LPVOID)This;
336 else if (IsEqualIID(riid, &IID_IBaseFilter))
337 *ppv = (LPVOID)This;
338 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
339 *ppv = (LPVOID)(&This->lpVtblFSF);
341 if (*ppv)
343 IUnknown_AddRef((IUnknown *)(*ppv));
344 return S_OK;
347 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
349 return E_NOINTERFACE;
352 static ULONG WINAPI AsyncReader_AddRef(IBaseFilter * iface)
354 ICOM_THIS(AsyncReader, iface);
356 TRACE("()\n");
358 return InterlockedIncrement(&This->refCount);
361 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
363 ICOM_THIS(AsyncReader, iface);
365 TRACE("()\n");
367 if (!InterlockedDecrement(&This->refCount))
369 if (This->pOutputPin)
370 IPin_Release(This->pOutputPin);
371 DeleteCriticalSection(&This->csFilter);
372 This->lpVtbl = NULL;
373 CoTaskMemFree(This);
374 return 0;
376 else
377 return This->refCount;
380 /** IPersist methods **/
382 static HRESULT WINAPI AsyncReader_GetClassID(IBaseFilter * iface, CLSID * pClsid)
384 TRACE("(%p)\n", pClsid);
386 *pClsid = CLSID_AsyncReader;
388 return S_OK;
391 /** IMediaFilter methods **/
393 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
395 ICOM_THIS(AsyncReader, iface);
397 TRACE("()\n");
399 This->state = State_Stopped;
401 return S_OK;
404 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
406 ICOM_THIS(AsyncReader, iface);
408 TRACE("()\n");
410 This->state = State_Paused;
412 return S_OK;
415 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
417 ICOM_THIS(AsyncReader, iface);
419 TRACE("(%lx%08lx)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
421 This->state = State_Running;
423 return S_OK;
426 static HRESULT WINAPI AsyncReader_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
428 ICOM_THIS(AsyncReader, iface);
430 TRACE("(%lu, %p)\n", dwMilliSecsTimeout, pState);
432 *pState = This->state;
434 return S_OK;
437 static HRESULT WINAPI AsyncReader_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
439 /* ICOM_THIS(AsyncReader, iface);*/
441 TRACE("(%p)\n", pClock);
443 return S_OK;
446 static HRESULT WINAPI AsyncReader_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
448 /* ICOM_THIS(AsyncReader, iface);*/
450 TRACE("(%p)\n", ppClock);
452 return S_OK;
455 /** IBaseFilter methods **/
457 static HRESULT WINAPI AsyncReader_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
459 ENUMPINDETAILS epd;
460 ICOM_THIS(AsyncReader, iface);
462 TRACE("(%p)\n", ppEnum);
464 epd.cPins = This->pOutputPin ? 1 : 0;
465 epd.ppPins = &This->pOutputPin;
466 return IEnumPinsImpl_Construct(&epd, ppEnum);
469 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
471 FIXME("(%s, %p)\n", debugstr_w(Id), ppPin);
473 return E_NOTIMPL;
476 static HRESULT WINAPI AsyncReader_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
478 ICOM_THIS(AsyncReader, iface);
480 TRACE("(%p)\n", pInfo);
482 strcpyW(pInfo->achName, This->filterInfo.achName);
483 pInfo->pGraph = This->filterInfo.pGraph;
485 if (pInfo->pGraph)
486 IFilterGraph_AddRef(pInfo->pGraph);
488 return S_OK;
491 static HRESULT WINAPI AsyncReader_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
493 ICOM_THIS(AsyncReader, iface);
495 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
497 if (pName)
498 strcpyW(This->filterInfo.achName, pName);
499 else
500 *This->filterInfo.achName = 0;
501 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
503 return S_OK;
506 static HRESULT WINAPI AsyncReader_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
508 FIXME("(%p)\n", pVendorInfo);
510 return E_NOTIMPL;
513 static const IBaseFilterVtbl AsyncReader_Vtbl =
515 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
516 AsyncReader_QueryInterface,
517 AsyncReader_AddRef,
518 AsyncReader_Release,
519 AsyncReader_GetClassID,
520 AsyncReader_Stop,
521 AsyncReader_Pause,
522 AsyncReader_Run,
523 AsyncReader_GetState,
524 AsyncReader_SetSyncSource,
525 AsyncReader_GetSyncSource,
526 AsyncReader_EnumPins,
527 AsyncReader_FindPin,
528 AsyncReader_QueryFilterInfo,
529 AsyncReader_JoinFilterGraph,
530 AsyncReader_QueryVendorInfo
533 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
535 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
537 return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->lpVtbl, riid, ppv);
540 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
542 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
544 return IBaseFilter_AddRef((IFileSourceFilter*)&This->lpVtbl);
547 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
549 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
551 return IBaseFilter_Release((IFileSourceFilter*)&This->lpVtbl);
554 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
556 HRESULT hr;
557 HANDLE hFile;
558 IAsyncReader * pReader = NULL;
559 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
561 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
563 /* open file */
564 /* FIXME: check the sharing values that native uses */
565 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
567 if (hFile == INVALID_HANDLE_VALUE)
569 return HRESULT_FROM_WIN32(GetLastError());
572 /* create pin */
573 hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->lpVtbl, &This->csFilter, &This->pOutputPin);
575 if (SUCCEEDED(hr))
576 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
578 /* store file name & media type */
579 if (SUCCEEDED(hr))
581 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
582 strcpyW(This->pszFileName, pszFileName);
583 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
584 if (!pmt)
586 This->pmt->bFixedSizeSamples = TRUE;
587 This->pmt->bTemporalCompression = FALSE;
588 This->pmt->cbFormat = 0;
589 This->pmt->pbFormat = NULL;
590 This->pmt->pUnk = NULL;
591 This->pmt->lSampleSize = 0;
592 memcpy(&This->pmt->formattype, &FORMAT_None, sizeof(FORMAT_None));
593 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype);
594 if (FAILED(hr))
596 CoTaskMemFree(This->pmt);
597 This->pmt = NULL;
600 else
601 CopyMediaType(This->pmt, pmt);
604 if (pReader)
605 IAsyncReader_Release(pReader);
607 if (FAILED(hr))
609 if (This->pOutputPin)
611 IPin_Release(This->pOutputPin);
612 This->pOutputPin = NULL;
614 if (This->pszFileName)
616 CoTaskMemFree(This->pszFileName);
617 This->pszFileName = NULL;
619 CloseHandle(hFile);
622 /* FIXME: check return codes */
623 return hr;
626 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
628 ICOM_THIS_From_IFileSourceFilter(AsyncReader, iface);
630 TRACE("(%p, %p)\n", ppszFileName, pmt);
632 /* copy file name & media type if available, otherwise clear the outputs */
633 if (This->pszFileName)
635 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
636 strcpyW(*ppszFileName, This->pszFileName);
638 else
639 *ppszFileName = NULL;
641 if (This->pmt)
643 CopyMediaType(pmt, This->pmt);
645 else
646 ZeroMemory(pmt, sizeof(*pmt));
648 return S_OK;
651 static const IFileSourceFilterVtbl FileSource_Vtbl =
653 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
654 FileSource_QueryInterface,
655 FileSource_AddRef,
656 FileSource_Release,
657 FileSource_Load,
658 FileSource_GetCurFile
662 /* the dwUserData passed back to user */
663 typedef struct DATAREQUEST
665 IMediaSample * pSample; /* sample passed to us by user */
666 DWORD_PTR dwUserData; /* user data passed to us */
667 OVERLAPPED ovl; /* our overlapped structure */
669 struct DATAREQUEST * pNext; /* next data request in list */
670 } DATAREQUEST;
672 void queue(DATAREQUEST * pHead, DATAREQUEST * pItem)
674 DATAREQUEST * pCurrent;
675 for (pCurrent = pHead; pCurrent->pNext; pCurrent = pCurrent->pNext)
677 pCurrent->pNext = pItem;
680 typedef struct FileAsyncReader
682 OutputPin pin;
683 const struct IAsyncReaderVtbl * lpVtblAR;
685 HANDLE hFile;
686 HANDLE hEvent;
687 BOOL bFlushing;
688 DATAREQUEST * pHead; /* head of data request list */
689 CRITICAL_SECTION csList; /* critical section to protect operations on list */
690 } FileAsyncReader;
692 static HRESULT AcceptProcAFR(LPVOID iface, const AM_MEDIA_TYPE *pmt)
694 ICOM_THIS(AsyncReader, iface);
696 FIXME("(%p, %p)\n", iface, pmt);
698 if (IsEqualGUID(&pmt->majortype, &This->pmt->majortype) &&
699 IsEqualGUID(&pmt->subtype, &This->pmt->subtype) &&
700 IsEqualGUID(&pmt->formattype, &FORMAT_None))
701 return S_OK;
703 return S_FALSE;
706 /* overriden pin functions */
708 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
710 ICOM_THIS(FileAsyncReader, iface);
711 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
713 *ppv = NULL;
715 if (IsEqualIID(riid, &IID_IUnknown))
716 *ppv = (LPVOID)This;
717 else if (IsEqualIID(riid, &IID_IPin))
718 *ppv = (LPVOID)This;
719 else if (IsEqualIID(riid, &IID_IAsyncReader))
720 *ppv = (LPVOID)&This->lpVtblAR;
722 if (*ppv)
724 IUnknown_AddRef((IUnknown *)(*ppv));
725 return S_OK;
728 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
730 return E_NOINTERFACE;
733 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
735 ICOM_THIS(FileAsyncReader, iface);
737 TRACE("()\n");
739 if (!InterlockedDecrement(&This->pin.pin.refCount))
741 DATAREQUEST * pCurrent;
742 DATAREQUEST * pNext;
743 for (pCurrent = This->pHead; pCurrent; pCurrent = pNext)
745 pNext = pCurrent->pNext;
746 CoTaskMemFree(pCurrent);
748 CloseHandle(This->hFile);
749 CloseHandle(This->hEvent);
750 CoTaskMemFree(This);
751 return 0;
753 return This->pin.pin.refCount;
756 static HRESULT WINAPI FileAsyncReaderPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
758 ENUMMEDIADETAILS emd;
759 ICOM_THIS(FileAsyncReader, iface);
761 TRACE("(%p)\n", ppEnum);
763 emd.cMediaTypes = 1;
764 emd.pMediaTypes = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
766 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
769 static HRESULT WINAPI FileAsyncReaderPin_Connect(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
771 HRESULT hr = S_OK;
772 ICOM_THIS(OutputPin, iface);
774 TRACE("(%p, %p)\n", pReceivePin, pmt);
775 dump_AM_MEDIA_TYPE(pmt);
777 /* If we try to connect to ourself, we will definitely deadlock.
778 * There are other cases where we could deadlock too, but this
779 * catches the obvious case */
780 assert(pReceivePin != iface);
782 EnterCriticalSection(This->pin.pCritSec);
784 /* if we have been a specific type to connect with, then we can either connect
785 * with that or fail. We cannot choose different AM_MEDIA_TYPE */
786 if (pmt && !IsEqualGUID(&pmt->majortype, &GUID_NULL) && !IsEqualGUID(&pmt->subtype, &GUID_NULL))
787 hr = This->pConnectSpecific(iface, pReceivePin, pmt);
788 else
790 /* negotiate media type */
792 IEnumMediaTypes * pEnumCandidates;
793 AM_MEDIA_TYPE * pmtCandidate; /* Candidate media type */
795 if (SUCCEEDED(IPin_EnumMediaTypes(iface, &pEnumCandidates)))
797 hr = VFW_E_NO_ACCEPTABLE_TYPES; /* Assume the worst, but set to S_OK if connected successfully */
799 /* try this filter's media types first */
800 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
802 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
803 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
805 hr = S_OK;
806 CoTaskMemFree(pmtCandidate);
807 break;
809 CoTaskMemFree(pmtCandidate);
811 IEnumMediaTypes_Release(pEnumCandidates);
814 /* then try receiver filter's media types */
815 if (hr != S_OK && SUCCEEDED(IPin_EnumMediaTypes(pReceivePin, &pEnumCandidates))) /* if we haven't already connected successfully */
817 while (S_OK == IEnumMediaTypes_Next(pEnumCandidates, 1, &pmtCandidate, NULL))
819 if (( !pmt || CompareMediaTypes(pmt, pmtCandidate, TRUE) ) &&
820 (This->pConnectSpecific(iface, pReceivePin, pmtCandidate) == S_OK))
822 hr = S_OK;
823 CoTaskMemFree(pmtCandidate);
824 break;
826 CoTaskMemFree(pmtCandidate);
827 } /* while */
828 IEnumMediaTypes_Release(pEnumCandidates);
829 } /* if not found */
830 } /* if negotiate media type */
831 } /* if succeeded */
832 LeaveCriticalSection(This->pin.pCritSec);
834 TRACE("-- %lx\n", hr);
835 return hr;
838 static const IPinVtbl FileAsyncReaderPin_Vtbl =
840 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
841 FileAsyncReaderPin_QueryInterface,
842 IPinImpl_AddRef,
843 FileAsyncReaderPin_Release,
844 FileAsyncReaderPin_Connect,
845 OutputPin_ReceiveConnection,
846 IPinImpl_Disconnect,
847 IPinImpl_ConnectedTo,
848 IPinImpl_ConnectionMediaType,
849 IPinImpl_QueryPinInfo,
850 IPinImpl_QueryDirection,
851 IPinImpl_QueryId,
852 IPinImpl_QueryAccept,
853 FileAsyncReaderPin_EnumMediaTypes,
854 IPinImpl_QueryInternalConnections,
855 OutputPin_EndOfStream,
856 OutputPin_BeginFlush,
857 OutputPin_EndFlush,
858 OutputPin_NewSegment
861 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
863 FileAsyncReader * pPinImpl;
864 PIN_INFO piOutput;
866 *ppPin = NULL;
868 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
870 if (!pPinImpl)
871 return E_OUTOFMEMORY;
873 piOutput.dir = PINDIR_OUTPUT;
874 piOutput.pFilter = pBaseFilter;
875 strcpyW(piOutput.achName, wszOutputPinName);
877 if (SUCCEEDED(OutputPin_Init(&piOutput, NULL, pBaseFilter, AcceptProcAFR, pCritSec, &pPinImpl->pin)))
879 pPinImpl->pin.pin.lpVtbl = &FileAsyncReaderPin_Vtbl;
880 pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
881 pPinImpl->hFile = hFile;
882 pPinImpl->hEvent = CreateEventW(NULL, 0, 0, NULL);
883 pPinImpl->bFlushing = FALSE;
884 pPinImpl->pHead = NULL;
886 *ppPin = (IPin *)(&pPinImpl->pin.pin.lpVtbl);
887 return S_OK;
889 return E_FAIL;
892 /* IAsyncReader */
894 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
896 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
898 return IPin_QueryInterface((IPin *)This, riid, ppv);
901 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
903 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
905 return IPin_AddRef((IPin *)This);
908 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
910 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
912 return IPin_Release((IPin *)This);
915 #define DEF_ALIGNMENT 1
917 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
919 HRESULT hr = S_OK;
921 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
923 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
924 pProps->cbAlign = DEF_ALIGNMENT;
926 if (pPreferred)
928 ALLOCATOR_PROPERTIES PropsActual;
929 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
930 /* FIXME: check we are still aligned */
931 if (SUCCEEDED(hr))
933 IMemAllocator_AddRef(pPreferred);
934 *ppActual = pPreferred;
935 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr);
936 return S_OK;
940 pPreferred = NULL;
942 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
944 if (SUCCEEDED(hr))
946 ALLOCATOR_PROPERTIES PropsActual;
947 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
948 /* FIXME: check we are still aligned */
949 if (SUCCEEDED(hr))
951 IMemAllocator_AddRef(pPreferred);
952 *ppActual = pPreferred;
953 TRACE("FileAsyncReader_RequestAllocator -- %lx\n", hr);
954 return S_OK;
958 if (FAILED(hr))
960 *ppActual = NULL;
961 if (pPreferred)
962 IMemAllocator_Release(pPreferred);
965 TRACE("-- %lx\n", hr);
966 return hr;
969 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
970 * however, this would be quite complicated to do and may be a bit error prone */
971 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
973 REFERENCE_TIME Start;
974 REFERENCE_TIME Stop;
975 DATAREQUEST * pDataRq;
976 BYTE * pBuffer;
977 HRESULT hr = S_OK;
978 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
980 TRACE("(%p, %lx)\n", pSample, dwUser);
982 /* check flushing state */
983 if (This->bFlushing)
984 return VFW_E_WRONG_STATE;
986 if (!(pDataRq = CoTaskMemAlloc(sizeof(*pDataRq))))
987 hr = E_OUTOFMEMORY;
989 /* get start and stop positions in bytes */
990 if (SUCCEEDED(hr))
991 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
993 if (SUCCEEDED(hr))
994 hr = IMediaSample_GetPointer(pSample, &pBuffer);
996 if (SUCCEEDED(hr))
998 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
1000 pDataRq->ovl.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
1001 pDataRq->ovl.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
1002 pDataRq->ovl.hEvent = This->hEvent;
1003 pDataRq->dwUserData = dwUser;
1004 pDataRq->pNext = NULL;
1005 /* we violate traditional COM rules here by maintaining
1006 * a reference to the sample, but not calling AddRef, but
1007 * that's what MSDN says to do */
1008 pDataRq->pSample = pSample;
1010 EnterCriticalSection(&This->csList);
1012 if (This->pHead)
1013 /* adds data request to end of list */
1014 queue(This->pHead, pDataRq);
1015 else
1016 This->pHead = pDataRq;
1018 LeaveCriticalSection(&This->csList);
1020 /* this is definitely not how it is implemented on Win9x
1021 * as they do not support async reads on files, but it is
1022 * sooo much easier to use this than messing around with threads!
1024 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1025 hr = HRESULT_FROM_WIN32(GetLastError());
1027 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1028 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1029 hr = S_OK;
1032 if (FAILED(hr) && pDataRq)
1034 EnterCriticalSection(&This->csList);
1036 DATAREQUEST * pCurrent;
1037 for (pCurrent = This->pHead; pCurrent && pCurrent->pNext; pCurrent = pCurrent->pNext)
1038 if (pCurrent->pNext == pDataRq)
1040 pCurrent->pNext = pDataRq->pNext;
1041 break;
1044 LeaveCriticalSection(&This->csList);
1045 CoTaskMemFree(pDataRq);
1048 TRACE("-- %lx\n", hr);
1049 return hr;
1052 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1054 HRESULT hr = S_OK;
1055 DATAREQUEST * pDataRq = NULL;
1056 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1058 TRACE("(%lu, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1060 /* FIXME: we could do with improving this by waiting for an array of event handles
1061 * and then determining which one finished and removing that from the list, otherwise
1062 * we will end up waiting for longer than we should do, if a later request finishes
1063 * before an earlier one */
1065 *ppSample = NULL;
1066 *pdwUser = 0;
1068 /* we return immediately if flushing */
1069 if (This->bFlushing)
1070 hr = VFW_E_WRONG_STATE;
1072 if (SUCCEEDED(hr))
1074 /* wait for the read to finish or timeout */
1075 if (WaitForSingleObject(This->hEvent, dwTimeout) == WAIT_TIMEOUT)
1076 hr = VFW_E_TIMEOUT;
1078 if (SUCCEEDED(hr))
1080 EnterCriticalSection(&This->csList);
1082 pDataRq = This->pHead;
1083 if (pDataRq == NULL)
1084 hr = E_FAIL;
1085 else
1086 This->pHead = pDataRq->pNext;
1088 LeaveCriticalSection(&This->csList);
1091 if (SUCCEEDED(hr))
1093 DWORD dwBytes;
1094 /* get any errors */
1095 if (!GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, TRUE))
1096 hr = HRESULT_FROM_WIN32(GetLastError());
1099 if (SUCCEEDED(hr))
1101 *ppSample = pDataRq->pSample;
1102 *pdwUser = pDataRq->dwUserData;
1105 /* clean up */
1106 if (pDataRq)
1108 /* no need to close event handle since we will close it when the pin is destroyed */
1109 CoTaskMemFree(pDataRq);
1112 TRACE("-- %lx\n", hr);
1113 return hr;
1116 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1118 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1120 BYTE * pBuffer;
1121 REFERENCE_TIME tStart;
1122 REFERENCE_TIME tStop;
1123 HRESULT hr;
1125 TRACE("(%p)\n", pSample);
1127 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1129 if (SUCCEEDED(hr))
1130 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1132 if (SUCCEEDED(hr))
1133 hr = FileAsyncReader_SyncRead(iface,
1134 BYTES_FROM_MEDIATIME(tStart),
1135 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1136 pBuffer);
1138 TRACE("-- %lx\n", hr);
1139 return hr;
1142 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1144 OVERLAPPED ovl;
1145 HRESULT hr = S_OK;
1146 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1148 TRACE("(%lx%08lx, %ld, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1150 ZeroMemory(&ovl, sizeof(ovl));
1152 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1153 /* NOTE: llPosition is the actual byte position to start reading from */
1154 ovl.Offset = (DWORD) llPosition;
1155 ovl.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1157 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1158 hr = HRESULT_FROM_WIN32(GetLastError());
1160 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1161 hr = S_OK;
1163 if (SUCCEEDED(hr))
1165 DWORD dwBytesRead;
1167 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1168 hr = HRESULT_FROM_WIN32(GetLastError());
1171 CloseHandle(ovl.hEvent);
1173 TRACE("-- %lx\n", hr);
1174 return hr;
1177 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1179 DWORD dwSizeLow;
1180 DWORD dwSizeHigh;
1181 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1183 TRACE("(%p, %p)\n", pTotal, pAvailable);
1185 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1186 (GetLastError() != NO_ERROR))
1187 return HRESULT_FROM_WIN32(GetLastError());
1189 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1191 *pAvailable = *pTotal;
1193 return S_OK;
1196 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1198 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1200 TRACE("()\n");
1202 This->bFlushing = TRUE;
1203 CancelIo(This->hFile);
1204 SetEvent(This->hEvent);
1206 /* FIXME: free list */
1208 return S_OK;
1211 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1213 ICOM_THIS_From_IAsyncReader(FileAsyncReader, iface);
1215 TRACE("()\n");
1217 This->bFlushing = FALSE;
1219 return S_OK;
1222 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1224 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1225 FileAsyncReader_QueryInterface,
1226 FileAsyncReader_AddRef,
1227 FileAsyncReader_Release,
1228 FileAsyncReader_RequestAllocator,
1229 FileAsyncReader_Request,
1230 FileAsyncReader_WaitForNext,
1231 FileAsyncReader_SyncReadAligned,
1232 FileAsyncReader_SyncRead,
1233 FileAsyncReader_Length,
1234 FileAsyncReader_BeginFlush,
1235 FileAsyncReader_EndFlush,