quartz: Reset EcCompleteCount before starting filters.
[wine/wine64.git] / dlls / quartz / filesource.c
blobfd977f9524a3ddf7581cda7d85a7bf17714b2e26
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
24 #include "quartz_private.h"
26 #include "wine/debug.h"
27 #include "wine/unicode.h"
28 #include "pin.h"
29 #include "uuids.h"
30 #include "vfwmsgs.h"
31 #include "winbase.h"
32 #include "winreg.h"
33 #include "shlwapi.h"
34 #include <assert.h>
36 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
38 static const WCHAR wszOutputPinName[] = { 'O','u','t','p','u','t',0 };
40 typedef struct AsyncReader
42 const IBaseFilterVtbl * lpVtbl;
43 const IFileSourceFilterVtbl * lpVtblFSF;
45 LONG refCount;
46 FILTER_INFO filterInfo;
47 FILTER_STATE state;
48 CRITICAL_SECTION csFilter;
50 IPin * pOutputPin;
51 LPOLESTR pszFileName;
52 AM_MEDIA_TYPE * pmt;
53 } AsyncReader;
55 static const IBaseFilterVtbl AsyncReader_Vtbl;
56 static const IFileSourceFilterVtbl FileSource_Vtbl;
57 static const IAsyncReaderVtbl FileAsyncReader_Vtbl;
59 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
61 static inline AsyncReader *impl_from_IFileSourceFilter( IFileSourceFilter *iface )
63 return (AsyncReader *)((char*)iface - FIELD_OFFSET(AsyncReader, lpVtblFSF));
66 static WCHAR const mediatype_name[11] = {
67 'M', 'e', 'd', 'i', 'a', ' ', 'T', 'y', 'p', 'e', 0 };
68 static WCHAR const subtype_name[8] = {
69 'S', 'u', 'b', 't', 'y', 'p', 'e', 0 };
71 static HRESULT process_extensions(HKEY hkeyExtensions, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
73 WCHAR *extension;
74 LONG l;
75 HKEY hsub;
76 WCHAR keying[39];
77 DWORD size;
79 if (!pszFileName)
80 return E_POINTER;
82 /* Get the part of the name that matters */
83 extension = PathFindExtensionW(pszFileName);
84 if (*extension != '.')
85 return E_FAIL;
87 l = RegOpenKeyExW(hkeyExtensions, extension, 0, KEY_READ, &hsub);
88 if (l)
89 return E_FAIL;
91 size = sizeof(keying);
92 l = RegQueryValueExW(hsub, mediatype_name, NULL, NULL, (LPBYTE)keying, &size);
93 if (!l)
94 CLSIDFromString(keying, majorType);
96 size = sizeof(keying);
97 if (!l)
98 l = RegQueryValueExW(hsub, subtype_name, NULL, NULL, (LPBYTE)keying, &size);
99 if (!l)
100 CLSIDFromString(keying, minorType);
102 RegCloseKey(hsub);
104 if (!l)
105 return S_OK;
106 return E_FAIL;
109 static unsigned char byte_from_hex_char(WCHAR wHex)
111 switch (tolowerW(wHex))
113 case '0':
114 case '1':
115 case '2':
116 case '3':
117 case '4':
118 case '5':
119 case '6':
120 case '7':
121 case '8':
122 case '9':
123 return (wHex - '0') & 0xf;
124 case 'a':
125 case 'b':
126 case 'c':
127 case 'd':
128 case 'e':
129 case 'f':
130 return (wHex - 'a' + 10) & 0xf;
131 default:
132 return 0;
136 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
138 ULONG ulOffset;
139 ULONG ulBytes;
140 BYTE * pbMask;
141 BYTE * pbValue;
142 BYTE * pbFile;
143 HRESULT hr = S_OK;
144 ULONG strpos;
146 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
148 /* format: "offset, bytestocompare, mask, value" */
150 ulOffset = strtolW(wszPatternString, NULL, 10);
152 if (!(wszPatternString = strchrW(wszPatternString, ',')))
153 return E_INVALIDARG;
155 wszPatternString++; /* skip ',' */
157 ulBytes = strtolW(wszPatternString, NULL, 10);
159 pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
160 pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
161 pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
163 /* default mask is match everything */
164 memset(pbMask, 0xFF, ulBytes);
166 if (!(wszPatternString = strchrW(wszPatternString, ',')))
167 hr = E_INVALIDARG;
169 wszPatternString++; /* skip ',' */
171 if (hr == S_OK)
173 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
176 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
178 if ((strpos % 2) == 1) /* odd numbered position */
179 pbMask[strpos / 2] |= byte_from_hex_char(*wszPatternString);
180 else
181 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
184 if (!(wszPatternString = strchrW(wszPatternString, ',')))
185 hr = E_INVALIDARG;
187 wszPatternString++; /* skip ',' */
190 if (hr == S_OK)
192 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
195 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
197 if ((strpos % 2) == 1) /* odd numbered position */
198 pbValue[strpos / 2] |= byte_from_hex_char(*wszPatternString);
199 else
200 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
204 if (hr == S_OK)
205 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
207 if (hr == S_OK)
209 ULONG i;
210 for (i = 0; i < ulBytes; i++)
211 if ((pbFile[i] & pbMask[i]) != pbValue[i])
213 hr = S_FALSE;
214 break;
218 HeapFree(GetProcessHeap(), 0, pbMask);
219 HeapFree(GetProcessHeap(), 0, pbValue);
220 HeapFree(GetProcessHeap(), 0, pbFile);
222 /* if we encountered no errors with this string, and there is a following tuple, then we
223 * have to match that as well to succeed */
224 if ((hr == S_OK) && (wszPatternString = strchrW(wszPatternString, ',')))
225 return process_pattern_string(wszPatternString + 1, pReader);
226 else
227 return hr;
230 static HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
232 HKEY hkeyMediaType = NULL;
233 LONG lRet;
234 HRESULT hr = S_OK;
235 BOOL bFound = FALSE;
236 static const WCHAR wszMediaType[] = {'M','e','d','i','a',' ','T','y','p','e',0};
238 TRACE("(%p, %s, %p, %p)\n", pReader, debugstr_w(pszFileName), majorType, minorType);
240 *majorType = GUID_NULL;
241 *minorType = GUID_NULL;
243 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType);
244 hr = HRESULT_FROM_WIN32(lRet);
246 if (SUCCEEDED(hr))
248 DWORD indexMajor;
250 for (indexMajor = 0; !bFound; indexMajor++)
252 HKEY hkeyMajor;
253 WCHAR wszMajorKeyName[CHARS_IN_GUID];
254 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
255 static const WCHAR wszExtensions[] = {'E','x','t','e','n','s','i','o','n','s',0};
257 if (RegEnumKeyExW(hkeyMediaType, indexMajor, wszMajorKeyName, &dwKeyNameLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
258 break;
259 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
260 break;
261 TRACE("%s\n", debugstr_w(wszMajorKeyName));
262 if (!strcmpW(wszExtensions, wszMajorKeyName))
264 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType) == S_OK)
265 bFound = TRUE;
267 else
269 DWORD indexMinor;
271 for (indexMinor = 0; !bFound; indexMinor++)
273 HKEY hkeyMinor;
274 WCHAR wszMinorKeyName[CHARS_IN_GUID];
275 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
276 DWORD maxValueLen;
277 DWORD indexValue;
279 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
280 break;
282 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
283 break;
285 TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
287 if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
288 break;
290 for (indexValue = 0; !bFound; indexValue++)
292 DWORD dwType;
293 WCHAR wszValueName[14]; /* longest name we should encounter will be "Source Filter" */
294 LPWSTR wszPatternString = HeapAlloc(GetProcessHeap(), 0, maxValueLen);
295 DWORD dwValueNameLen = sizeof(wszValueName) / sizeof(wszValueName[0]); /* remember this is in chars */
296 DWORD dwDataLen = maxValueLen; /* remember this is in bytes */
297 static const WCHAR wszSourceFilter[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
298 LONG temp;
300 if ((temp = RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen)) != ERROR_SUCCESS)
302 HeapFree(GetProcessHeap(), 0, wszPatternString);
303 break;
306 /* if it is not the source filter value */
307 if (strcmpW(wszValueName, wszSourceFilter))
309 if (process_pattern_string(wszPatternString, pReader) == S_OK)
311 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName, majorType)) &&
312 SUCCEEDED(CLSIDFromString(wszMinorKeyName, minorType)))
313 bFound = TRUE;
316 HeapFree(GetProcessHeap(), 0, wszPatternString);
318 CloseHandle(hkeyMinor);
321 CloseHandle(hkeyMajor);
324 CloseHandle(hkeyMediaType);
326 if (SUCCEEDED(hr) && !bFound)
328 ERR("Media class not found\n");
329 hr = E_FAIL;
331 else if (bFound)
332 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType), qzdebugstr_guid(minorType));
334 return hr;
337 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
339 AsyncReader *pAsyncRead;
341 if( pUnkOuter )
342 return CLASS_E_NOAGGREGATION;
344 pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
346 if (!pAsyncRead)
347 return E_OUTOFMEMORY;
349 pAsyncRead->lpVtbl = &AsyncReader_Vtbl;
350 pAsyncRead->lpVtblFSF = &FileSource_Vtbl;
351 pAsyncRead->refCount = 1;
352 pAsyncRead->filterInfo.achName[0] = '\0';
353 pAsyncRead->filterInfo.pGraph = NULL;
354 pAsyncRead->pOutputPin = NULL;
356 InitializeCriticalSection(&pAsyncRead->csFilter);
357 pAsyncRead->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": AsyncReader.csFilter");
359 pAsyncRead->pszFileName = NULL;
360 pAsyncRead->pmt = NULL;
362 *ppv = (LPVOID)pAsyncRead;
364 TRACE("-- created at %p\n", pAsyncRead);
366 return S_OK;
369 /** IUnknown methods **/
371 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
373 AsyncReader *This = (AsyncReader *)iface;
375 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
377 *ppv = NULL;
379 if (IsEqualIID(riid, &IID_IUnknown))
380 *ppv = (LPVOID)This;
381 else if (IsEqualIID(riid, &IID_IPersist))
382 *ppv = (LPVOID)This;
383 else if (IsEqualIID(riid, &IID_IMediaFilter))
384 *ppv = (LPVOID)This;
385 else if (IsEqualIID(riid, &IID_IBaseFilter))
386 *ppv = (LPVOID)This;
387 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
388 *ppv = (LPVOID)(&This->lpVtblFSF);
390 if (*ppv)
392 IUnknown_AddRef((IUnknown *)(*ppv));
393 return S_OK;
396 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking))
397 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
399 return E_NOINTERFACE;
402 static ULONG WINAPI AsyncReader_AddRef(IBaseFilter * iface)
404 AsyncReader *This = (AsyncReader *)iface;
405 ULONG refCount = InterlockedIncrement(&This->refCount);
407 TRACE("(%p)->() AddRef from %d\n", This, refCount - 1);
409 return refCount;
412 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
414 AsyncReader *This = (AsyncReader *)iface;
415 ULONG refCount = InterlockedDecrement(&This->refCount);
417 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
419 if (!refCount)
421 if (This->pOutputPin)
423 IPin *pConnectedTo;
424 if(SUCCEEDED(IPin_ConnectedTo(This->pOutputPin, &pConnectedTo)))
426 IPin_Disconnect(pConnectedTo);
427 IPin_Release(pConnectedTo);
429 IPin_Disconnect(This->pOutputPin);
430 IPin_Release(This->pOutputPin);
432 This->csFilter.DebugInfo->Spare[0] = 0;
433 DeleteCriticalSection(&This->csFilter);
434 This->lpVtbl = NULL;
435 CoTaskMemFree(This->pszFileName);
436 FreeMediaType(This->pmt);
437 CoTaskMemFree(This);
438 return 0;
440 else
441 return refCount;
444 /** IPersist methods **/
446 static HRESULT WINAPI AsyncReader_GetClassID(IBaseFilter * iface, CLSID * pClsid)
448 TRACE("(%p)\n", pClsid);
450 *pClsid = CLSID_AsyncReader;
452 return S_OK;
455 /** IMediaFilter methods **/
457 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
459 AsyncReader *This = (AsyncReader *)iface;
461 TRACE("()\n");
463 This->state = State_Stopped;
465 return S_OK;
468 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
470 AsyncReader *This = (AsyncReader *)iface;
472 TRACE("()\n");
474 This->state = State_Paused;
476 return S_OK;
479 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
481 AsyncReader *This = (AsyncReader *)iface;
483 TRACE("(%x%08x)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
485 This->state = State_Running;
487 return S_OK;
490 static HRESULT WINAPI AsyncReader_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
492 AsyncReader *This = (AsyncReader *)iface;
494 TRACE("(%u, %p)\n", dwMilliSecsTimeout, pState);
496 *pState = This->state;
498 return S_OK;
501 static HRESULT WINAPI AsyncReader_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
503 /* AsyncReader *This = (AsyncReader *)iface;*/
505 TRACE("(%p)\n", pClock);
507 return S_OK;
510 static HRESULT WINAPI AsyncReader_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
512 /* AsyncReader *This = (AsyncReader *)iface;*/
514 TRACE("(%p)\n", ppClock);
516 return S_OK;
519 /** IBaseFilter methods **/
521 static HRESULT WINAPI AsyncReader_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
523 ENUMPINDETAILS epd;
524 AsyncReader *This = (AsyncReader *)iface;
526 TRACE("(%p)\n", ppEnum);
528 epd.cPins = This->pOutputPin ? 1 : 0;
529 epd.ppPins = &This->pOutputPin;
530 return IEnumPinsImpl_Construct(&epd, ppEnum);
533 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
535 FIXME("(%s, %p)\n", debugstr_w(Id), ppPin);
537 return E_NOTIMPL;
540 static HRESULT WINAPI AsyncReader_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
542 AsyncReader *This = (AsyncReader *)iface;
544 TRACE("(%p)\n", pInfo);
546 strcpyW(pInfo->achName, This->filterInfo.achName);
547 pInfo->pGraph = This->filterInfo.pGraph;
549 if (pInfo->pGraph)
550 IFilterGraph_AddRef(pInfo->pGraph);
552 return S_OK;
555 static HRESULT WINAPI AsyncReader_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
557 AsyncReader *This = (AsyncReader *)iface;
559 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
561 if (pName)
562 strcpyW(This->filterInfo.achName, pName);
563 else
564 *This->filterInfo.achName = 0;
565 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
567 return S_OK;
570 static HRESULT WINAPI AsyncReader_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
572 FIXME("(%p)\n", pVendorInfo);
574 return E_NOTIMPL;
577 static const IBaseFilterVtbl AsyncReader_Vtbl =
579 AsyncReader_QueryInterface,
580 AsyncReader_AddRef,
581 AsyncReader_Release,
582 AsyncReader_GetClassID,
583 AsyncReader_Stop,
584 AsyncReader_Pause,
585 AsyncReader_Run,
586 AsyncReader_GetState,
587 AsyncReader_SetSyncSource,
588 AsyncReader_GetSyncSource,
589 AsyncReader_EnumPins,
590 AsyncReader_FindPin,
591 AsyncReader_QueryFilterInfo,
592 AsyncReader_JoinFilterGraph,
593 AsyncReader_QueryVendorInfo
596 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
598 AsyncReader *This = impl_from_IFileSourceFilter(iface);
600 return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->lpVtbl, riid, ppv);
603 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
605 AsyncReader *This = impl_from_IFileSourceFilter(iface);
607 return IBaseFilter_AddRef((IFileSourceFilter*)&This->lpVtbl);
610 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
612 AsyncReader *This = impl_from_IFileSourceFilter(iface);
614 return IBaseFilter_Release((IFileSourceFilter*)&This->lpVtbl);
617 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
619 HRESULT hr;
620 HANDLE hFile;
621 IAsyncReader * pReader = NULL;
622 AsyncReader *This = impl_from_IFileSourceFilter(iface);
624 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
626 /* open file */
627 /* FIXME: check the sharing values that native uses */
628 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
630 if (hFile == INVALID_HANDLE_VALUE)
632 return HRESULT_FROM_WIN32(GetLastError());
635 /* create pin */
636 hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->lpVtbl, &This->csFilter, &This->pOutputPin);
638 if (SUCCEEDED(hr))
639 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
641 /* store file name & media type */
642 if (SUCCEEDED(hr))
644 CoTaskMemFree(This->pszFileName);
645 if (This->pmt)
646 FreeMediaType(This->pmt);
648 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
649 strcpyW(This->pszFileName, pszFileName);
651 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
652 if (!pmt)
654 This->pmt->bFixedSizeSamples = TRUE;
655 This->pmt->bTemporalCompression = FALSE;
656 This->pmt->cbFormat = 0;
657 This->pmt->pbFormat = NULL;
658 This->pmt->pUnk = NULL;
659 This->pmt->lSampleSize = 0;
660 This->pmt->formattype = FORMAT_None;
661 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype);
662 if (FAILED(hr))
664 CoTaskMemFree(This->pmt);
665 This->pmt = NULL;
668 else
669 CopyMediaType(This->pmt, pmt);
672 if (pReader)
673 IAsyncReader_Release(pReader);
675 if (FAILED(hr))
677 if (This->pOutputPin)
679 IPin_Release(This->pOutputPin);
680 This->pOutputPin = NULL;
683 CoTaskMemFree(This->pszFileName);
684 FreeMediaType(This->pmt);
685 This->pszFileName = NULL;
686 This->pmt = NULL;
688 CloseHandle(hFile);
691 /* FIXME: check return codes */
692 return hr;
695 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
697 AsyncReader *This = impl_from_IFileSourceFilter(iface);
699 TRACE("(%p, %p)\n", ppszFileName, pmt);
701 if (!ppszFileName)
702 return E_POINTER;
704 /* copy file name & media type if available, otherwise clear the outputs */
705 if (This->pszFileName)
707 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
708 strcpyW(*ppszFileName, This->pszFileName);
710 else
711 *ppszFileName = NULL;
713 if (pmt)
715 if (This->pmt)
716 CopyMediaType(pmt, This->pmt);
717 else
718 ZeroMemory(pmt, sizeof(*pmt));
721 return S_OK;
724 static const IFileSourceFilterVtbl FileSource_Vtbl =
726 FileSource_QueryInterface,
727 FileSource_AddRef,
728 FileSource_Release,
729 FileSource_Load,
730 FileSource_GetCurFile
734 /* the dwUserData passed back to user */
735 typedef struct DATAREQUEST
737 IMediaSample * pSample; /* sample passed to us by user */
738 DWORD_PTR dwUserData; /* user data passed to us */
739 OVERLAPPED ovl; /* our overlapped structure */
741 struct DATAREQUEST * pNext; /* next data request in list */
742 } DATAREQUEST;
744 static void queue(DATAREQUEST * pHead, DATAREQUEST * pItem)
746 DATAREQUEST * pCurrent;
747 for (pCurrent = pHead; pCurrent->pNext; pCurrent = pCurrent->pNext)
749 pCurrent->pNext = pItem;
752 typedef struct FileAsyncReader
754 OutputPin pin;
755 const struct IAsyncReaderVtbl * lpVtblAR;
757 HANDLE hFile;
758 HANDLE hEvent;
759 BOOL bFlushing;
760 DATAREQUEST * pHead; /* head of data request list */
761 CRITICAL_SECTION csList; /* critical section to protect operations on list */
762 } FileAsyncReader;
764 static inline FileAsyncReader *impl_from_IAsyncReader( IAsyncReader *iface )
766 return (FileAsyncReader *)((char*)iface - FIELD_OFFSET(FileAsyncReader, lpVtblAR));
769 static HRESULT AcceptProcAFR(LPVOID iface, const AM_MEDIA_TYPE *pmt)
771 AsyncReader *This = (AsyncReader *)iface;
773 FIXME("(%p, %p)\n", iface, pmt);
775 if (IsEqualGUID(&pmt->majortype, &This->pmt->majortype) &&
776 IsEqualGUID(&pmt->subtype, &This->pmt->subtype) &&
777 IsEqualGUID(&pmt->formattype, &FORMAT_None))
778 return S_OK;
780 return S_FALSE;
783 /* overriden pin functions */
785 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
787 FileAsyncReader *This = (FileAsyncReader *)iface;
788 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
790 *ppv = NULL;
792 if (IsEqualIID(riid, &IID_IUnknown))
793 *ppv = (LPVOID)This;
794 else if (IsEqualIID(riid, &IID_IPin))
795 *ppv = (LPVOID)This;
796 else if (IsEqualIID(riid, &IID_IAsyncReader))
797 *ppv = (LPVOID)&This->lpVtblAR;
799 if (*ppv)
801 IUnknown_AddRef((IUnknown *)(*ppv));
802 return S_OK;
805 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking))
806 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
808 return E_NOINTERFACE;
811 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
813 FileAsyncReader *This = (FileAsyncReader *)iface;
814 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
816 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
818 if (!refCount)
820 DATAREQUEST * pCurrent;
821 DATAREQUEST * pNext;
822 for (pCurrent = This->pHead; pCurrent; pCurrent = pNext)
824 pNext = pCurrent->pNext;
825 CoTaskMemFree(pCurrent);
827 CloseHandle(This->hFile);
828 CloseHandle(This->hEvent);
829 This->csList.DebugInfo->Spare[0] = 0;
830 DeleteCriticalSection(&This->csList);
831 CoTaskMemFree(This);
832 return 0;
834 return refCount;
837 static HRESULT WINAPI FileAsyncReaderPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
839 ENUMMEDIADETAILS emd;
840 FileAsyncReader *This = (FileAsyncReader *)iface;
842 TRACE("(%p)\n", ppEnum);
844 emd.cMediaTypes = 1;
845 emd.pMediaTypes = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
847 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
850 static const IPinVtbl FileAsyncReaderPin_Vtbl =
852 FileAsyncReaderPin_QueryInterface,
853 IPinImpl_AddRef,
854 FileAsyncReaderPin_Release,
855 OutputPin_Connect,
856 OutputPin_ReceiveConnection,
857 IPinImpl_Disconnect,
858 IPinImpl_ConnectedTo,
859 IPinImpl_ConnectionMediaType,
860 IPinImpl_QueryPinInfo,
861 IPinImpl_QueryDirection,
862 IPinImpl_QueryId,
863 IPinImpl_QueryAccept,
864 FileAsyncReaderPin_EnumMediaTypes,
865 IPinImpl_QueryInternalConnections,
866 OutputPin_EndOfStream,
867 OutputPin_BeginFlush,
868 OutputPin_EndFlush,
869 OutputPin_NewSegment
872 /* Function called as a helper to IPin_Connect */
873 /* specific AM_MEDIA_TYPE - it cannot be NULL */
874 /* this differs from standard OutputPin_ConnectSpecific only in that it
875 * doesn't need the IMemInputPin interface on the receiving pin */
876 static HRESULT FileAsyncReaderPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
878 OutputPin *This = (OutputPin *)iface;
879 HRESULT hr;
881 TRACE("(%p, %p)\n", pReceivePin, pmt);
882 dump_AM_MEDIA_TYPE(pmt);
884 /* FIXME: call queryacceptproc */
886 This->pin.pConnectedTo = pReceivePin;
887 IPin_AddRef(pReceivePin);
888 CopyMediaType(&This->pin.mtCurrent, pmt);
890 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
892 if (FAILED(hr))
894 IPin_Release(This->pin.pConnectedTo);
895 This->pin.pConnectedTo = NULL;
896 FreeMediaType(&This->pin.mtCurrent);
899 TRACE(" -- %x\n", hr);
900 return hr;
903 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
905 PIN_INFO piOutput;
906 HRESULT hr;
908 *ppPin = NULL;
909 piOutput.dir = PINDIR_OUTPUT;
910 piOutput.pFilter = pBaseFilter;
911 strcpyW(piOutput.achName, wszOutputPinName);
912 hr = OutputPin_Construct(&FileAsyncReaderPin_Vtbl, sizeof(FileAsyncReader), &piOutput, NULL, pBaseFilter, AcceptProcAFR, pCritSec, ppPin);
914 if (SUCCEEDED(hr))
916 FileAsyncReader *pPinImpl = (FileAsyncReader *)*ppPin;
917 pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
918 pPinImpl->hFile = hFile;
919 pPinImpl->hEvent = CreateEventW(NULL, 0, 0, NULL);
920 pPinImpl->bFlushing = FALSE;
921 pPinImpl->pHead = NULL;
922 pPinImpl->pin.pConnectSpecific = FileAsyncReaderPin_ConnectSpecific;
923 InitializeCriticalSection(&pPinImpl->csList);
924 pPinImpl->csList.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FileAsyncReader.csList");
926 return hr;
929 /* IAsyncReader */
931 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
933 FileAsyncReader *This = impl_from_IAsyncReader(iface);
935 return IPin_QueryInterface((IPin *)This, riid, ppv);
938 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
940 FileAsyncReader *This = impl_from_IAsyncReader(iface);
942 return IPin_AddRef((IPin *)This);
945 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
947 FileAsyncReader *This = impl_from_IAsyncReader(iface);
949 return IPin_Release((IPin *)This);
952 #define DEF_ALIGNMENT 1
954 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
956 HRESULT hr = S_OK;
958 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
960 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
961 pProps->cbAlign = DEF_ALIGNMENT;
963 if (pPreferred)
965 ALLOCATOR_PROPERTIES PropsActual;
966 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
967 /* FIXME: check we are still aligned */
968 if (SUCCEEDED(hr))
970 IMemAllocator_AddRef(pPreferred);
971 *ppActual = pPreferred;
972 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
973 return S_OK;
977 pPreferred = NULL;
979 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
981 if (SUCCEEDED(hr))
983 ALLOCATOR_PROPERTIES PropsActual;
984 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
985 /* FIXME: check we are still aligned */
986 if (SUCCEEDED(hr))
988 *ppActual = pPreferred;
989 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
990 return S_OK;
994 if (FAILED(hr))
996 *ppActual = NULL;
997 if (pPreferred)
998 IMemAllocator_Release(pPreferred);
1001 TRACE("-- %x\n", hr);
1002 return hr;
1005 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
1006 * however, this would be quite complicated to do and may be a bit error prone */
1007 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
1009 REFERENCE_TIME Start;
1010 REFERENCE_TIME Stop;
1011 DATAREQUEST * pDataRq;
1012 BYTE * pBuffer;
1013 HRESULT hr = S_OK;
1014 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1016 TRACE("(%p, %lx)\n", pSample, dwUser);
1018 /* check flushing state */
1019 if (This->bFlushing)
1020 return VFW_E_WRONG_STATE;
1022 if (!(pDataRq = CoTaskMemAlloc(sizeof(*pDataRq))))
1023 hr = E_OUTOFMEMORY;
1025 /* get start and stop positions in bytes */
1026 if (SUCCEEDED(hr))
1027 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
1029 if (SUCCEEDED(hr))
1030 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1032 if (SUCCEEDED(hr))
1034 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
1036 pDataRq->ovl.u.s.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
1037 pDataRq->ovl.u.s.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
1038 pDataRq->ovl.hEvent = This->hEvent;
1039 pDataRq->dwUserData = dwUser;
1040 pDataRq->pNext = NULL;
1041 /* we violate traditional COM rules here by maintaining
1042 * a reference to the sample, but not calling AddRef, but
1043 * that's what MSDN says to do */
1044 pDataRq->pSample = pSample;
1046 EnterCriticalSection(&This->csList);
1048 if (This->pHead)
1049 /* adds data request to end of list */
1050 queue(This->pHead, pDataRq);
1051 else
1052 This->pHead = pDataRq;
1054 LeaveCriticalSection(&This->csList);
1056 /* this is definitely not how it is implemented on Win9x
1057 * as they do not support async reads on files, but it is
1058 * sooo much easier to use this than messing around with threads!
1060 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1061 hr = HRESULT_FROM_WIN32(GetLastError());
1063 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1064 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1065 hr = S_OK;
1068 if (FAILED(hr) && pDataRq)
1070 EnterCriticalSection(&This->csList);
1072 DATAREQUEST * pCurrent;
1073 for (pCurrent = This->pHead; pCurrent && pCurrent->pNext; pCurrent = pCurrent->pNext)
1074 if (pCurrent->pNext == pDataRq)
1076 pCurrent->pNext = pDataRq->pNext;
1077 break;
1080 LeaveCriticalSection(&This->csList);
1081 CoTaskMemFree(pDataRq);
1084 TRACE("-- %x\n", hr);
1085 return hr;
1088 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1090 HRESULT hr = S_OK;
1091 DWORD dwBytes = 0;
1092 DATAREQUEST * pDataRq = NULL;
1093 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1095 TRACE("(%u, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1097 /* FIXME: we could do with improving this by waiting for an array of event handles
1098 * and then determining which one finished and removing that from the list, otherwise
1099 * we will end up waiting for longer than we should do, if a later request finishes
1100 * before an earlier one */
1102 *ppSample = NULL;
1103 *pdwUser = 0;
1105 if (!This->bFlushing)
1107 /* wait for the read to finish or timeout */
1108 if (WaitForSingleObject(This->hEvent, dwTimeout) == WAIT_TIMEOUT)
1109 hr = VFW_E_TIMEOUT;
1112 if (SUCCEEDED(hr))
1114 EnterCriticalSection(&This->csList);
1116 pDataRq = This->pHead;
1117 if (pDataRq == NULL)
1118 hr = E_FAIL;
1119 else
1120 This->pHead = pDataRq->pNext;
1122 LeaveCriticalSection(&This->csList);
1125 if (SUCCEEDED(hr) && !This->bFlushing)
1127 /* get any errors */
1128 if (!GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1129 hr = HRESULT_FROM_WIN32(GetLastError());
1132 if (SUCCEEDED(hr))
1134 IMediaSample_SetActualDataLength(pDataRq->pSample, dwBytes);
1135 *ppSample = pDataRq->pSample;
1136 *pdwUser = pDataRq->dwUserData;
1139 /* no need to close event handle since we will close it when the pin is destroyed */
1140 CoTaskMemFree(pDataRq);
1142 /* Return the sample if flushing so it can be destroyed */
1143 if (This->bFlushing && SUCCEEDED(hr))
1145 hr = VFW_E_WRONG_STATE;
1146 IMediaSample_SetActualDataLength(pDataRq->pSample, 0);
1149 TRACE("-- %x\n", hr);
1150 return hr;
1153 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1155 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1157 BYTE * pBuffer;
1158 REFERENCE_TIME tStart;
1159 REFERENCE_TIME tStop;
1160 HRESULT hr;
1162 TRACE("(%p)\n", pSample);
1164 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1166 if (SUCCEEDED(hr))
1167 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1169 if (SUCCEEDED(hr))
1170 hr = FileAsyncReader_SyncRead(iface,
1171 BYTES_FROM_MEDIATIME(tStart),
1172 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1173 pBuffer);
1175 TRACE("-- %x\n", hr);
1176 return hr;
1179 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1181 OVERLAPPED ovl;
1182 HRESULT hr = S_OK;
1183 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1185 TRACE("(%x%08x, %d, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1187 ZeroMemory(&ovl, sizeof(ovl));
1189 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1190 /* NOTE: llPosition is the actual byte position to start reading from */
1191 ovl.u.s.Offset = (DWORD) llPosition;
1192 ovl.u.s.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1194 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1195 hr = HRESULT_FROM_WIN32(GetLastError());
1197 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1198 hr = S_OK;
1200 if (SUCCEEDED(hr))
1202 DWORD dwBytesRead;
1204 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1205 hr = HRESULT_FROM_WIN32(GetLastError());
1208 CloseHandle(ovl.hEvent);
1210 TRACE("-- %x\n", hr);
1211 return hr;
1214 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1216 DWORD dwSizeLow;
1217 DWORD dwSizeHigh;
1218 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1220 TRACE("(%p, %p)\n", pTotal, pAvailable);
1222 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1223 (GetLastError() != NO_ERROR))
1224 return HRESULT_FROM_WIN32(GetLastError());
1226 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1228 *pAvailable = *pTotal;
1230 return S_OK;
1233 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1235 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1237 TRACE("()\n");
1239 This->bFlushing = TRUE;
1240 CancelIo(This->hFile);
1241 SetEvent(This->hEvent);
1243 /* FIXME: free list */
1245 return S_OK;
1248 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1250 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1252 TRACE("()\n");
1254 This->bFlushing = FALSE;
1256 return S_OK;
1259 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1261 FileAsyncReader_QueryInterface,
1262 FileAsyncReader_AddRef,
1263 FileAsyncReader_Release,
1264 FileAsyncReader_RequestAllocator,
1265 FileAsyncReader_Request,
1266 FileAsyncReader_WaitForNext,
1267 FileAsyncReader_SyncReadAligned,
1268 FileAsyncReader_SyncRead,
1269 FileAsyncReader_Length,
1270 FileAsyncReader_BeginFlush,
1271 FileAsyncReader_EndFlush,