quartz: Add DebugInfo to critical sections.
[wine/hacks.git] / dlls / quartz / filesource.c
blob22209f5e629dc5e1b6f32551323fe3657bc55f5f
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 <assert.h>
35 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
37 static const WCHAR wszOutputPinName[] = { 'O','u','t','p','u','t',0 };
39 typedef struct AsyncReader
41 const IBaseFilterVtbl * lpVtbl;
42 const IFileSourceFilterVtbl * lpVtblFSF;
44 LONG 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 IBaseFilterVtbl AsyncReader_Vtbl;
55 static const IFileSourceFilterVtbl FileSource_Vtbl;
56 static const IAsyncReaderVtbl FileAsyncReader_Vtbl;
58 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
60 static inline AsyncReader *impl_from_IFileSourceFilter( IFileSourceFilter *iface )
62 return (AsyncReader *)((char*)iface - FIELD_OFFSET(AsyncReader, lpVtblFSF));
65 static HRESULT process_extensions(HKEY hkeyExtensions, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
67 /* FIXME: implement */
68 return E_NOTIMPL;
71 static unsigned char byte_from_hex_char(WCHAR wHex)
73 switch (tolowerW(wHex))
75 case '0':
76 case '1':
77 case '2':
78 case '3':
79 case '4':
80 case '5':
81 case '6':
82 case '7':
83 case '8':
84 case '9':
85 return (wHex - '0') & 0xf;
86 case 'a':
87 case 'b':
88 case 'c':
89 case 'd':
90 case 'e':
91 case 'f':
92 return (wHex - 'a' + 10) & 0xf;
93 default:
94 return 0;
98 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
100 ULONG ulOffset;
101 ULONG ulBytes;
102 BYTE * pbMask;
103 BYTE * pbValue;
104 BYTE * pbFile;
105 HRESULT hr = S_OK;
106 ULONG strpos;
108 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
110 /* format: "offset, bytestocompare, mask, value" */
112 ulOffset = strtolW(wszPatternString, NULL, 10);
114 if (!(wszPatternString = strchrW(wszPatternString, ',')))
115 return E_INVALIDARG;
117 wszPatternString++; /* skip ',' */
119 ulBytes = strtolW(wszPatternString, NULL, 10);
121 pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
122 pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
123 pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
125 /* default mask is match everything */
126 memset(pbMask, 0xFF, ulBytes);
128 if (!(wszPatternString = strchrW(wszPatternString, ',')))
129 hr = E_INVALIDARG;
131 wszPatternString++; /* skip ',' */
133 if (hr == S_OK)
135 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
138 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
140 if ((strpos % 2) == 1) /* odd numbered position */
141 pbMask[strpos / 2] |= byte_from_hex_char(*wszPatternString);
142 else
143 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
146 if (!(wszPatternString = strchrW(wszPatternString, ',')))
147 hr = E_INVALIDARG;
149 wszPatternString++; /* skip ',' */
152 if (hr == S_OK)
154 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
157 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
159 if ((strpos % 2) == 1) /* odd numbered position */
160 pbValue[strpos / 2] |= byte_from_hex_char(*wszPatternString);
161 else
162 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
166 if (hr == S_OK)
167 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
169 if (hr == S_OK)
171 ULONG i;
172 for (i = 0; i < ulBytes; i++)
173 if ((pbFile[i] & pbMask[i]) != pbValue[i])
175 hr = S_FALSE;
176 break;
180 HeapFree(GetProcessHeap(), 0, pbMask);
181 HeapFree(GetProcessHeap(), 0, pbValue);
182 HeapFree(GetProcessHeap(), 0, pbFile);
184 /* if we encountered no errors with this string, and there is a following tuple, then we
185 * have to match that as well to succeed */
186 if ((hr == S_OK) && (wszPatternString = strchrW(wszPatternString, ',')))
187 return process_pattern_string(wszPatternString + 1, pReader);
188 else
189 return hr;
192 static HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
194 HKEY hkeyMediaType = NULL;
195 LONG lRet;
196 HRESULT hr = S_OK;
197 BOOL bFound = FALSE;
198 static const 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 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType);
204 hr = HRESULT_FROM_WIN32(lRet);
206 if (SUCCEEDED(hr))
208 DWORD indexMajor;
210 for (indexMajor = 0; !bFound; indexMajor++)
212 HKEY hkeyMajor;
213 WCHAR wszMajorKeyName[CHARS_IN_GUID];
214 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
215 static const WCHAR wszExtensions[] = {'E','x','t','e','n','s','i','o','n','s',0};
217 if (RegEnumKeyExW(hkeyMediaType, indexMajor, wszMajorKeyName, &dwKeyNameLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
218 break;
219 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
220 break;
221 TRACE("%s\n", debugstr_w(wszMajorKeyName));
222 if (!strcmpW(wszExtensions, wszMajorKeyName))
224 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType) == S_OK)
225 bFound = TRUE;
227 else
229 DWORD indexMinor;
231 for (indexMinor = 0; !bFound; indexMinor++)
233 HKEY hkeyMinor;
234 WCHAR wszMinorKeyName[CHARS_IN_GUID];
235 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
236 DWORD maxValueLen;
237 DWORD indexValue;
239 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
240 break;
242 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
243 break;
245 TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
247 if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
248 break;
250 for (indexValue = 0; !bFound; indexValue++)
252 DWORD dwType;
253 WCHAR wszValueName[14]; /* longest name we should encounter will be "Source Filter" */
254 LPWSTR wszPatternString = HeapAlloc(GetProcessHeap(), 0, maxValueLen);
255 DWORD dwValueNameLen = sizeof(wszValueName) / sizeof(wszValueName[0]); /* remember this is in chars */
256 DWORD dwDataLen = maxValueLen; /* remember this is in bytes */
257 static const WCHAR wszSourceFilter[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
258 LONG temp;
260 if ((temp = RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen)) != ERROR_SUCCESS)
262 HeapFree(GetProcessHeap(), 0, wszPatternString);
263 break;
266 /* if it is not the source filter value */
267 if (strcmpW(wszValueName, wszSourceFilter))
269 if (process_pattern_string(wszPatternString, pReader) == S_OK)
271 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName, majorType)) &&
272 SUCCEEDED(CLSIDFromString(wszMinorKeyName, minorType)))
273 bFound = TRUE;
276 HeapFree(GetProcessHeap(), 0, wszPatternString);
278 CloseHandle(hkeyMinor);
281 CloseHandle(hkeyMajor);
284 CloseHandle(hkeyMediaType);
286 if (SUCCEEDED(hr) && !bFound)
288 ERR("Media class not found\n");
289 hr = E_FAIL;
291 else if (bFound)
292 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType), qzdebugstr_guid(minorType));
294 return hr;
297 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
299 AsyncReader *pAsyncRead;
301 if( pUnkOuter )
302 return CLASS_E_NOAGGREGATION;
304 pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
306 if (!pAsyncRead)
307 return E_OUTOFMEMORY;
309 pAsyncRead->lpVtbl = &AsyncReader_Vtbl;
310 pAsyncRead->lpVtblFSF = &FileSource_Vtbl;
311 pAsyncRead->refCount = 1;
312 pAsyncRead->filterInfo.achName[0] = '\0';
313 pAsyncRead->filterInfo.pGraph = NULL;
314 pAsyncRead->pOutputPin = NULL;
316 InitializeCriticalSection(&pAsyncRead->csFilter);
317 pAsyncRead->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": AsyncReader.csFilter");
319 pAsyncRead->pszFileName = NULL;
320 pAsyncRead->pmt = NULL;
322 *ppv = (LPVOID)pAsyncRead;
324 TRACE("-- created at %p\n", pAsyncRead);
326 return S_OK;
329 /** IUnknown methods **/
331 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
333 AsyncReader *This = (AsyncReader *)iface;
335 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
337 *ppv = NULL;
339 if (IsEqualIID(riid, &IID_IUnknown))
340 *ppv = (LPVOID)This;
341 else if (IsEqualIID(riid, &IID_IPersist))
342 *ppv = (LPVOID)This;
343 else if (IsEqualIID(riid, &IID_IMediaFilter))
344 *ppv = (LPVOID)This;
345 else if (IsEqualIID(riid, &IID_IBaseFilter))
346 *ppv = (LPVOID)This;
347 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
348 *ppv = (LPVOID)(&This->lpVtblFSF);
350 if (*ppv)
352 IUnknown_AddRef((IUnknown *)(*ppv));
353 return S_OK;
356 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
358 return E_NOINTERFACE;
361 static ULONG WINAPI AsyncReader_AddRef(IBaseFilter * iface)
363 AsyncReader *This = (AsyncReader *)iface;
364 ULONG refCount = InterlockedIncrement(&This->refCount);
366 TRACE("(%p)->() AddRef from %d\n", This, refCount - 1);
368 return refCount;
371 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
373 AsyncReader *This = (AsyncReader *)iface;
374 ULONG refCount = InterlockedDecrement(&This->refCount);
376 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
378 if (!refCount)
380 if (This->pOutputPin)
381 IPin_Release(This->pOutputPin);
382 This->csFilter.DebugInfo->Spare[0] = 0;
383 DeleteCriticalSection(&This->csFilter);
384 This->lpVtbl = NULL;
385 CoTaskMemFree(This);
386 return 0;
388 else
389 return refCount;
392 /** IPersist methods **/
394 static HRESULT WINAPI AsyncReader_GetClassID(IBaseFilter * iface, CLSID * pClsid)
396 TRACE("(%p)\n", pClsid);
398 *pClsid = CLSID_AsyncReader;
400 return S_OK;
403 /** IMediaFilter methods **/
405 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
407 AsyncReader *This = (AsyncReader *)iface;
409 TRACE("()\n");
411 This->state = State_Stopped;
413 return S_OK;
416 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
418 AsyncReader *This = (AsyncReader *)iface;
420 TRACE("()\n");
422 This->state = State_Paused;
424 return S_OK;
427 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
429 AsyncReader *This = (AsyncReader *)iface;
431 TRACE("(%x%08x)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
433 This->state = State_Running;
435 return S_OK;
438 static HRESULT WINAPI AsyncReader_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
440 AsyncReader *This = (AsyncReader *)iface;
442 TRACE("(%u, %p)\n", dwMilliSecsTimeout, pState);
444 *pState = This->state;
446 return S_OK;
449 static HRESULT WINAPI AsyncReader_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
451 /* AsyncReader *This = (AsyncReader *)iface;*/
453 TRACE("(%p)\n", pClock);
455 return S_OK;
458 static HRESULT WINAPI AsyncReader_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
460 /* AsyncReader *This = (AsyncReader *)iface;*/
462 TRACE("(%p)\n", ppClock);
464 return S_OK;
467 /** IBaseFilter methods **/
469 static HRESULT WINAPI AsyncReader_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
471 ENUMPINDETAILS epd;
472 AsyncReader *This = (AsyncReader *)iface;
474 TRACE("(%p)\n", ppEnum);
476 epd.cPins = This->pOutputPin ? 1 : 0;
477 epd.ppPins = &This->pOutputPin;
478 return IEnumPinsImpl_Construct(&epd, ppEnum);
481 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
483 FIXME("(%s, %p)\n", debugstr_w(Id), ppPin);
485 return E_NOTIMPL;
488 static HRESULT WINAPI AsyncReader_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
490 AsyncReader *This = (AsyncReader *)iface;
492 TRACE("(%p)\n", pInfo);
494 strcpyW(pInfo->achName, This->filterInfo.achName);
495 pInfo->pGraph = This->filterInfo.pGraph;
497 if (pInfo->pGraph)
498 IFilterGraph_AddRef(pInfo->pGraph);
500 return S_OK;
503 static HRESULT WINAPI AsyncReader_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
505 AsyncReader *This = (AsyncReader *)iface;
507 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
509 if (pName)
510 strcpyW(This->filterInfo.achName, pName);
511 else
512 *This->filterInfo.achName = 0;
513 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
515 return S_OK;
518 static HRESULT WINAPI AsyncReader_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
520 FIXME("(%p)\n", pVendorInfo);
522 return E_NOTIMPL;
525 static const IBaseFilterVtbl AsyncReader_Vtbl =
527 AsyncReader_QueryInterface,
528 AsyncReader_AddRef,
529 AsyncReader_Release,
530 AsyncReader_GetClassID,
531 AsyncReader_Stop,
532 AsyncReader_Pause,
533 AsyncReader_Run,
534 AsyncReader_GetState,
535 AsyncReader_SetSyncSource,
536 AsyncReader_GetSyncSource,
537 AsyncReader_EnumPins,
538 AsyncReader_FindPin,
539 AsyncReader_QueryFilterInfo,
540 AsyncReader_JoinFilterGraph,
541 AsyncReader_QueryVendorInfo
544 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
546 AsyncReader *This = impl_from_IFileSourceFilter(iface);
548 return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->lpVtbl, riid, ppv);
551 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
553 AsyncReader *This = impl_from_IFileSourceFilter(iface);
555 return IBaseFilter_AddRef((IFileSourceFilter*)&This->lpVtbl);
558 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
560 AsyncReader *This = impl_from_IFileSourceFilter(iface);
562 return IBaseFilter_Release((IFileSourceFilter*)&This->lpVtbl);
565 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
567 HRESULT hr;
568 HANDLE hFile;
569 IAsyncReader * pReader = NULL;
570 AsyncReader *This = impl_from_IFileSourceFilter(iface);
572 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
574 /* open file */
575 /* FIXME: check the sharing values that native uses */
576 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
578 if (hFile == INVALID_HANDLE_VALUE)
580 return HRESULT_FROM_WIN32(GetLastError());
583 /* create pin */
584 hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->lpVtbl, &This->csFilter, &This->pOutputPin);
586 if (SUCCEEDED(hr))
587 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
589 /* store file name & media type */
590 if (SUCCEEDED(hr))
592 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
593 strcpyW(This->pszFileName, pszFileName);
594 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
595 if (!pmt)
597 This->pmt->bFixedSizeSamples = TRUE;
598 This->pmt->bTemporalCompression = FALSE;
599 This->pmt->cbFormat = 0;
600 This->pmt->pbFormat = NULL;
601 This->pmt->pUnk = NULL;
602 This->pmt->lSampleSize = 0;
603 memcpy(&This->pmt->formattype, &FORMAT_None, sizeof(FORMAT_None));
604 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype);
605 if (FAILED(hr))
607 CoTaskMemFree(This->pmt);
608 This->pmt = NULL;
611 else
612 CopyMediaType(This->pmt, pmt);
615 if (pReader)
616 IAsyncReader_Release(pReader);
618 if (FAILED(hr))
620 if (This->pOutputPin)
622 IPin_Release(This->pOutputPin);
623 This->pOutputPin = NULL;
626 CoTaskMemFree(This->pszFileName);
627 This->pszFileName = NULL;
629 CloseHandle(hFile);
632 /* FIXME: check return codes */
633 return hr;
636 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
638 AsyncReader *This = impl_from_IFileSourceFilter(iface);
640 TRACE("(%p, %p)\n", ppszFileName, pmt);
642 if (!ppszFileName)
643 return E_POINTER;
645 /* copy file name & media type if available, otherwise clear the outputs */
646 if (This->pszFileName)
648 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
649 strcpyW(*ppszFileName, This->pszFileName);
651 else
652 *ppszFileName = NULL;
654 if (pmt)
656 if (This->pmt)
657 CopyMediaType(pmt, This->pmt);
658 else
659 ZeroMemory(pmt, sizeof(*pmt));
662 return S_OK;
665 static const IFileSourceFilterVtbl FileSource_Vtbl =
667 FileSource_QueryInterface,
668 FileSource_AddRef,
669 FileSource_Release,
670 FileSource_Load,
671 FileSource_GetCurFile
675 /* the dwUserData passed back to user */
676 typedef struct DATAREQUEST
678 IMediaSample * pSample; /* sample passed to us by user */
679 DWORD_PTR dwUserData; /* user data passed to us */
680 OVERLAPPED ovl; /* our overlapped structure */
682 struct DATAREQUEST * pNext; /* next data request in list */
683 } DATAREQUEST;
685 static void queue(DATAREQUEST * pHead, DATAREQUEST * pItem)
687 DATAREQUEST * pCurrent;
688 for (pCurrent = pHead; pCurrent->pNext; pCurrent = pCurrent->pNext)
690 pCurrent->pNext = pItem;
693 typedef struct FileAsyncReader
695 OutputPin pin;
696 const struct IAsyncReaderVtbl * lpVtblAR;
698 HANDLE hFile;
699 HANDLE hEvent;
700 BOOL bFlushing;
701 DATAREQUEST * pHead; /* head of data request list */
702 CRITICAL_SECTION csList; /* critical section to protect operations on list */
703 } FileAsyncReader;
705 static inline FileAsyncReader *impl_from_IAsyncReader( IAsyncReader *iface )
707 return (FileAsyncReader *)((char*)iface - FIELD_OFFSET(FileAsyncReader, lpVtblAR));
710 static HRESULT AcceptProcAFR(LPVOID iface, const AM_MEDIA_TYPE *pmt)
712 AsyncReader *This = (AsyncReader *)iface;
714 FIXME("(%p, %p)\n", iface, pmt);
716 if (IsEqualGUID(&pmt->majortype, &This->pmt->majortype) &&
717 IsEqualGUID(&pmt->subtype, &This->pmt->subtype) &&
718 IsEqualGUID(&pmt->formattype, &FORMAT_None))
719 return S_OK;
721 return S_FALSE;
724 /* overriden pin functions */
726 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
728 FileAsyncReader *This = (FileAsyncReader *)iface;
729 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
731 *ppv = NULL;
733 if (IsEqualIID(riid, &IID_IUnknown))
734 *ppv = (LPVOID)This;
735 else if (IsEqualIID(riid, &IID_IPin))
736 *ppv = (LPVOID)This;
737 else if (IsEqualIID(riid, &IID_IAsyncReader))
738 *ppv = (LPVOID)&This->lpVtblAR;
740 if (*ppv)
742 IUnknown_AddRef((IUnknown *)(*ppv));
743 return S_OK;
746 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
748 return E_NOINTERFACE;
751 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
753 FileAsyncReader *This = (FileAsyncReader *)iface;
754 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
756 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
758 if (!refCount)
760 DATAREQUEST * pCurrent;
761 DATAREQUEST * pNext;
762 for (pCurrent = This->pHead; pCurrent; pCurrent = pNext)
764 pNext = pCurrent->pNext;
765 CoTaskMemFree(pCurrent);
767 CloseHandle(This->hFile);
768 CloseHandle(This->hEvent);
769 This->csList.DebugInfo->Spare[0] = 0;
770 DeleteCriticalSection(&This->csList);
771 CoTaskMemFree(This);
772 return 0;
774 return refCount;
777 static HRESULT WINAPI FileAsyncReaderPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
779 ENUMMEDIADETAILS emd;
780 FileAsyncReader *This = (FileAsyncReader *)iface;
782 TRACE("(%p)\n", ppEnum);
784 emd.cMediaTypes = 1;
785 emd.pMediaTypes = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
787 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
790 static const IPinVtbl FileAsyncReaderPin_Vtbl =
792 FileAsyncReaderPin_QueryInterface,
793 IPinImpl_AddRef,
794 FileAsyncReaderPin_Release,
795 OutputPin_Connect,
796 OutputPin_ReceiveConnection,
797 IPinImpl_Disconnect,
798 IPinImpl_ConnectedTo,
799 IPinImpl_ConnectionMediaType,
800 IPinImpl_QueryPinInfo,
801 IPinImpl_QueryDirection,
802 IPinImpl_QueryId,
803 IPinImpl_QueryAccept,
804 FileAsyncReaderPin_EnumMediaTypes,
805 IPinImpl_QueryInternalConnections,
806 OutputPin_EndOfStream,
807 OutputPin_BeginFlush,
808 OutputPin_EndFlush,
809 OutputPin_NewSegment
812 /* Function called as a helper to IPin_Connect */
813 /* specific AM_MEDIA_TYPE - it cannot be NULL */
814 /* this differs from standard OutputPin_ConnectSpecific only in that it
815 * doesn't need the IMemInputPin interface on the receiving pin */
816 static HRESULT FileAsyncReaderPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
818 OutputPin *This = (OutputPin *)iface;
819 HRESULT hr;
821 TRACE("(%p, %p)\n", pReceivePin, pmt);
822 dump_AM_MEDIA_TYPE(pmt);
824 /* FIXME: call queryacceptproc */
826 This->pin.pConnectedTo = pReceivePin;
827 IPin_AddRef(pReceivePin);
828 CopyMediaType(&This->pin.mtCurrent, pmt);
830 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
832 if (FAILED(hr))
834 IPin_Release(This->pin.pConnectedTo);
835 This->pin.pConnectedTo = NULL;
836 FreeMediaType(&This->pin.mtCurrent);
839 TRACE(" -- %x\n", hr);
840 return hr;
843 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
845 FileAsyncReader * pPinImpl;
846 PIN_INFO piOutput;
848 *ppPin = NULL;
850 pPinImpl = CoTaskMemAlloc(sizeof(*pPinImpl));
852 if (!pPinImpl)
853 return E_OUTOFMEMORY;
855 piOutput.dir = PINDIR_OUTPUT;
856 piOutput.pFilter = pBaseFilter;
857 strcpyW(piOutput.achName, wszOutputPinName);
859 if (SUCCEEDED(OutputPin_Init(&piOutput, NULL, pBaseFilter, AcceptProcAFR, pCritSec, &pPinImpl->pin)))
861 pPinImpl->pin.pin.lpVtbl = &FileAsyncReaderPin_Vtbl;
862 pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
863 pPinImpl->hFile = hFile;
864 pPinImpl->hEvent = CreateEventW(NULL, 0, 0, NULL);
865 pPinImpl->bFlushing = FALSE;
866 pPinImpl->pHead = NULL;
867 pPinImpl->pin.pConnectSpecific = FileAsyncReaderPin_ConnectSpecific;
868 InitializeCriticalSection(&pPinImpl->csList);
869 pPinImpl->csList.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FileAsyncReader.csList");
871 *ppPin = (IPin *)(&pPinImpl->pin.pin.lpVtbl);
872 return S_OK;
874 return E_FAIL;
877 /* IAsyncReader */
879 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
881 FileAsyncReader *This = impl_from_IAsyncReader(iface);
883 return IPin_QueryInterface((IPin *)This, riid, ppv);
886 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
888 FileAsyncReader *This = impl_from_IAsyncReader(iface);
890 return IPin_AddRef((IPin *)This);
893 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
895 FileAsyncReader *This = impl_from_IAsyncReader(iface);
897 return IPin_Release((IPin *)This);
900 #define DEF_ALIGNMENT 1
902 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
904 HRESULT hr = S_OK;
906 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
908 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
909 pProps->cbAlign = DEF_ALIGNMENT;
911 if (pPreferred)
913 ALLOCATOR_PROPERTIES PropsActual;
914 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
915 /* FIXME: check we are still aligned */
916 if (SUCCEEDED(hr))
918 IMemAllocator_AddRef(pPreferred);
919 *ppActual = pPreferred;
920 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
921 return S_OK;
925 pPreferred = NULL;
927 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
929 if (SUCCEEDED(hr))
931 ALLOCATOR_PROPERTIES PropsActual;
932 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
933 /* FIXME: check we are still aligned */
934 if (SUCCEEDED(hr))
936 *ppActual = pPreferred;
937 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
938 return S_OK;
942 if (FAILED(hr))
944 *ppActual = NULL;
945 if (pPreferred)
946 IMemAllocator_Release(pPreferred);
949 TRACE("-- %x\n", hr);
950 return hr;
953 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
954 * however, this would be quite complicated to do and may be a bit error prone */
955 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
957 REFERENCE_TIME Start;
958 REFERENCE_TIME Stop;
959 DATAREQUEST * pDataRq;
960 BYTE * pBuffer;
961 HRESULT hr = S_OK;
962 FileAsyncReader *This = impl_from_IAsyncReader(iface);
964 TRACE("(%p, %lx)\n", pSample, dwUser);
966 /* check flushing state */
967 if (This->bFlushing)
968 return VFW_E_WRONG_STATE;
970 if (!(pDataRq = CoTaskMemAlloc(sizeof(*pDataRq))))
971 hr = E_OUTOFMEMORY;
973 /* get start and stop positions in bytes */
974 if (SUCCEEDED(hr))
975 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
977 if (SUCCEEDED(hr))
978 hr = IMediaSample_GetPointer(pSample, &pBuffer);
980 if (SUCCEEDED(hr))
982 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
984 pDataRq->ovl.u.s.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
985 pDataRq->ovl.u.s.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
986 pDataRq->ovl.hEvent = This->hEvent;
987 pDataRq->dwUserData = dwUser;
988 pDataRq->pNext = NULL;
989 /* we violate traditional COM rules here by maintaining
990 * a reference to the sample, but not calling AddRef, but
991 * that's what MSDN says to do */
992 pDataRq->pSample = pSample;
994 EnterCriticalSection(&This->csList);
996 if (This->pHead)
997 /* adds data request to end of list */
998 queue(This->pHead, pDataRq);
999 else
1000 This->pHead = pDataRq;
1002 LeaveCriticalSection(&This->csList);
1004 /* this is definitely not how it is implemented on Win9x
1005 * as they do not support async reads on files, but it is
1006 * sooo much easier to use this than messing around with threads!
1008 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1009 hr = HRESULT_FROM_WIN32(GetLastError());
1011 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1012 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1013 hr = S_OK;
1016 if (FAILED(hr) && pDataRq)
1018 EnterCriticalSection(&This->csList);
1020 DATAREQUEST * pCurrent;
1021 for (pCurrent = This->pHead; pCurrent && pCurrent->pNext; pCurrent = pCurrent->pNext)
1022 if (pCurrent->pNext == pDataRq)
1024 pCurrent->pNext = pDataRq->pNext;
1025 break;
1028 LeaveCriticalSection(&This->csList);
1029 CoTaskMemFree(pDataRq);
1032 TRACE("-- %x\n", hr);
1033 return hr;
1036 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1038 HRESULT hr = S_OK;
1039 DATAREQUEST * pDataRq = NULL;
1040 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1042 TRACE("(%u, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1044 /* FIXME: we could do with improving this by waiting for an array of event handles
1045 * and then determining which one finished and removing that from the list, otherwise
1046 * we will end up waiting for longer than we should do, if a later request finishes
1047 * before an earlier one */
1049 *ppSample = NULL;
1050 *pdwUser = 0;
1052 /* we return immediately if flushing */
1053 if (This->bFlushing)
1054 hr = VFW_E_WRONG_STATE;
1056 if (SUCCEEDED(hr))
1058 /* wait for the read to finish or timeout */
1059 if (WaitForSingleObject(This->hEvent, dwTimeout) == WAIT_TIMEOUT)
1060 hr = VFW_E_TIMEOUT;
1062 if (SUCCEEDED(hr))
1064 EnterCriticalSection(&This->csList);
1066 pDataRq = This->pHead;
1067 if (pDataRq == NULL)
1068 hr = E_FAIL;
1069 else
1070 This->pHead = pDataRq->pNext;
1072 LeaveCriticalSection(&This->csList);
1075 if (SUCCEEDED(hr))
1077 DWORD dwBytes;
1078 /* get any errors */
1079 if (!GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1080 hr = HRESULT_FROM_WIN32(GetLastError());
1083 if (SUCCEEDED(hr))
1085 *ppSample = pDataRq->pSample;
1086 *pdwUser = pDataRq->dwUserData;
1089 /* no need to close event handle since we will close it when the pin is destroyed */
1090 CoTaskMemFree(pDataRq);
1092 TRACE("-- %x\n", hr);
1093 return hr;
1096 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1098 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1100 BYTE * pBuffer;
1101 REFERENCE_TIME tStart;
1102 REFERENCE_TIME tStop;
1103 HRESULT hr;
1105 TRACE("(%p)\n", pSample);
1107 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1109 if (SUCCEEDED(hr))
1110 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1112 if (SUCCEEDED(hr))
1113 hr = FileAsyncReader_SyncRead(iface,
1114 BYTES_FROM_MEDIATIME(tStart),
1115 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1116 pBuffer);
1118 TRACE("-- %x\n", hr);
1119 return hr;
1122 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1124 OVERLAPPED ovl;
1125 HRESULT hr = S_OK;
1126 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1128 TRACE("(%x%08x, %d, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1130 ZeroMemory(&ovl, sizeof(ovl));
1132 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1133 /* NOTE: llPosition is the actual byte position to start reading from */
1134 ovl.u.s.Offset = (DWORD) llPosition;
1135 ovl.u.s.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1137 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1138 hr = HRESULT_FROM_WIN32(GetLastError());
1140 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1141 hr = S_OK;
1143 if (SUCCEEDED(hr))
1145 DWORD dwBytesRead;
1147 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1148 hr = HRESULT_FROM_WIN32(GetLastError());
1151 CloseHandle(ovl.hEvent);
1153 TRACE("-- %x\n", hr);
1154 return hr;
1157 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1159 DWORD dwSizeLow;
1160 DWORD dwSizeHigh;
1161 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1163 TRACE("(%p, %p)\n", pTotal, pAvailable);
1165 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1166 (GetLastError() != NO_ERROR))
1167 return HRESULT_FROM_WIN32(GetLastError());
1169 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1171 *pAvailable = *pTotal;
1173 return S_OK;
1176 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1178 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1180 TRACE("()\n");
1182 This->bFlushing = TRUE;
1183 CancelIo(This->hFile);
1184 SetEvent(This->hEvent);
1186 /* FIXME: free list */
1188 return S_OK;
1191 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1193 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1195 TRACE("()\n");
1197 This->bFlushing = FALSE;
1199 return S_OK;
1202 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1204 FileAsyncReader_QueryInterface,
1205 FileAsyncReader_AddRef,
1206 FileAsyncReader_Release,
1207 FileAsyncReader_RequestAllocator,
1208 FileAsyncReader_Request,
1209 FileAsyncReader_WaitForNext,
1210 FileAsyncReader_SyncReadAligned,
1211 FileAsyncReader_SyncRead,
1212 FileAsyncReader_Length,
1213 FileAsyncReader_BeginFlush,
1214 FileAsyncReader_EndFlush,