push 6fe5edf8439c19d3885814583531c2f2b1495177
[wine/hacks.git] / dlls / quartz / filesource.c
blobd31e1502e51974eef679728d79aca0aeb4b93bcb
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);
100 if (!l)
101 CLSIDFromString(keying, minorType);
103 RegCloseKey(hsub);
105 if (!l)
106 return S_OK;
107 return E_FAIL;
110 static unsigned char byte_from_hex_char(WCHAR wHex)
112 switch (tolowerW(wHex))
114 case '0':
115 case '1':
116 case '2':
117 case '3':
118 case '4':
119 case '5':
120 case '6':
121 case '7':
122 case '8':
123 case '9':
124 return (wHex - '0') & 0xf;
125 case 'a':
126 case 'b':
127 case 'c':
128 case 'd':
129 case 'e':
130 case 'f':
131 return (wHex - 'a' + 10) & 0xf;
132 default:
133 return 0;
137 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
139 ULONG ulOffset;
140 ULONG ulBytes;
141 BYTE * pbMask;
142 BYTE * pbValue;
143 BYTE * pbFile;
144 HRESULT hr = S_OK;
145 ULONG strpos;
147 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
149 /* format: "offset, bytestocompare, mask, value" */
151 ulOffset = strtolW(wszPatternString, NULL, 10);
153 if (!(wszPatternString = strchrW(wszPatternString, ',')))
154 return E_INVALIDARG;
156 wszPatternString++; /* skip ',' */
158 ulBytes = strtolW(wszPatternString, NULL, 10);
160 pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
161 pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
162 pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
164 /* default mask is match everything */
165 memset(pbMask, 0xFF, ulBytes);
167 if (!(wszPatternString = strchrW(wszPatternString, ',')))
168 hr = E_INVALIDARG;
170 wszPatternString++; /* skip ',' */
172 if (hr == S_OK)
174 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
177 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
179 if ((strpos % 2) == 1) /* odd numbered position */
180 pbMask[strpos / 2] |= byte_from_hex_char(*wszPatternString);
181 else
182 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
185 if (!(wszPatternString = strchrW(wszPatternString, ',')))
186 hr = E_INVALIDARG;
188 wszPatternString++; /* skip ',' */
191 if (hr == S_OK)
193 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
196 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
198 if ((strpos % 2) == 1) /* odd numbered position */
199 pbValue[strpos / 2] |= byte_from_hex_char(*wszPatternString);
200 else
201 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
205 if (hr == S_OK)
206 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
208 if (hr == S_OK)
210 ULONG i;
211 for (i = 0; i < ulBytes; i++)
212 if ((pbFile[i] & pbMask[i]) != pbValue[i])
214 hr = S_FALSE;
215 break;
219 HeapFree(GetProcessHeap(), 0, pbMask);
220 HeapFree(GetProcessHeap(), 0, pbValue);
221 HeapFree(GetProcessHeap(), 0, pbFile);
223 /* if we encountered no errors with this string, and there is a following tuple, then we
224 * have to match that as well to succeed */
225 if ((hr == S_OK) && (wszPatternString = strchrW(wszPatternString, ',')))
226 return process_pattern_string(wszPatternString + 1, pReader);
227 else
228 return hr;
231 static HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
233 HKEY hkeyMediaType = NULL;
234 LONG lRet;
235 HRESULT hr = S_OK;
236 BOOL bFound = FALSE;
237 static const WCHAR wszMediaType[] = {'M','e','d','i','a',' ','T','y','p','e',0};
239 TRACE("(%p, %s, %p, %p)\n", pReader, debugstr_w(pszFileName), majorType, minorType);
241 *majorType = GUID_NULL;
242 *minorType = GUID_NULL;
244 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType);
245 hr = HRESULT_FROM_WIN32(lRet);
247 if (SUCCEEDED(hr))
249 DWORD indexMajor;
251 for (indexMajor = 0; !bFound; indexMajor++)
253 HKEY hkeyMajor;
254 WCHAR wszMajorKeyName[CHARS_IN_GUID];
255 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
256 static const WCHAR wszExtensions[] = {'E','x','t','e','n','s','i','o','n','s',0};
258 if (RegEnumKeyExW(hkeyMediaType, indexMajor, wszMajorKeyName, &dwKeyNameLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
259 break;
260 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
261 break;
262 TRACE("%s\n", debugstr_w(wszMajorKeyName));
263 if (!strcmpW(wszExtensions, wszMajorKeyName))
265 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType) == S_OK)
266 bFound = TRUE;
268 else
270 DWORD indexMinor;
272 for (indexMinor = 0; !bFound; indexMinor++)
274 HKEY hkeyMinor;
275 WCHAR wszMinorKeyName[CHARS_IN_GUID];
276 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
277 DWORD maxValueLen;
278 DWORD indexValue;
280 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
281 break;
283 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
284 break;
286 TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
288 if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
289 break;
291 for (indexValue = 0; !bFound; indexValue++)
293 DWORD dwType;
294 WCHAR wszValueName[14]; /* longest name we should encounter will be "Source Filter" */
295 LPWSTR wszPatternString = HeapAlloc(GetProcessHeap(), 0, maxValueLen);
296 DWORD dwValueNameLen = sizeof(wszValueName) / sizeof(wszValueName[0]); /* remember this is in chars */
297 DWORD dwDataLen = maxValueLen; /* remember this is in bytes */
298 static const WCHAR wszSourceFilter[] = {'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
299 LONG temp;
301 if ((temp = RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen)) != ERROR_SUCCESS)
303 HeapFree(GetProcessHeap(), 0, wszPatternString);
304 break;
307 /* if it is not the source filter value */
308 if (strcmpW(wszValueName, wszSourceFilter))
310 if (process_pattern_string(wszPatternString, pReader) == S_OK)
312 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName, majorType)) &&
313 SUCCEEDED(CLSIDFromString(wszMinorKeyName, minorType)))
314 bFound = TRUE;
317 HeapFree(GetProcessHeap(), 0, wszPatternString);
319 CloseHandle(hkeyMinor);
322 CloseHandle(hkeyMajor);
325 CloseHandle(hkeyMediaType);
327 if (SUCCEEDED(hr) && !bFound)
329 ERR("Media class not found\n");
330 hr = E_FAIL;
332 else if (bFound)
333 TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType), qzdebugstr_guid(minorType));
335 return hr;
338 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
340 AsyncReader *pAsyncRead;
342 if( pUnkOuter )
343 return CLASS_E_NOAGGREGATION;
345 pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
347 if (!pAsyncRead)
348 return E_OUTOFMEMORY;
350 pAsyncRead->lpVtbl = &AsyncReader_Vtbl;
351 pAsyncRead->lpVtblFSF = &FileSource_Vtbl;
352 pAsyncRead->refCount = 1;
353 pAsyncRead->filterInfo.achName[0] = '\0';
354 pAsyncRead->filterInfo.pGraph = NULL;
355 pAsyncRead->pOutputPin = NULL;
357 InitializeCriticalSection(&pAsyncRead->csFilter);
358 pAsyncRead->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": AsyncReader.csFilter");
360 pAsyncRead->pszFileName = NULL;
361 pAsyncRead->pmt = NULL;
363 *ppv = (LPVOID)pAsyncRead;
365 TRACE("-- created at %p\n", pAsyncRead);
367 return S_OK;
370 /** IUnknown methods **/
372 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
374 AsyncReader *This = (AsyncReader *)iface;
376 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
378 *ppv = NULL;
380 if (IsEqualIID(riid, &IID_IUnknown))
381 *ppv = (LPVOID)This;
382 else if (IsEqualIID(riid, &IID_IPersist))
383 *ppv = (LPVOID)This;
384 else if (IsEqualIID(riid, &IID_IMediaFilter))
385 *ppv = (LPVOID)This;
386 else if (IsEqualIID(riid, &IID_IBaseFilter))
387 *ppv = (LPVOID)This;
388 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
389 *ppv = (LPVOID)(&This->lpVtblFSF);
391 if (*ppv)
393 IUnknown_AddRef((IUnknown *)(*ppv));
394 return S_OK;
397 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking) && !IsEqualIID(riid, &IID_IVideoWindow))
398 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
400 return E_NOINTERFACE;
403 static ULONG WINAPI AsyncReader_AddRef(IBaseFilter * iface)
405 AsyncReader *This = (AsyncReader *)iface;
406 ULONG refCount = InterlockedIncrement(&This->refCount);
408 TRACE("(%p)->() AddRef from %d\n", This, refCount - 1);
410 return refCount;
413 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
415 AsyncReader *This = (AsyncReader *)iface;
416 ULONG refCount = InterlockedDecrement(&This->refCount);
418 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
420 if (!refCount)
422 if (This->pOutputPin)
424 IPin *pConnectedTo;
425 if(SUCCEEDED(IPin_ConnectedTo(This->pOutputPin, &pConnectedTo)))
427 IPin_Disconnect(pConnectedTo);
428 IPin_Release(pConnectedTo);
430 IPin_Disconnect(This->pOutputPin);
431 IPin_Release(This->pOutputPin);
433 This->csFilter.DebugInfo->Spare[0] = 0;
434 DeleteCriticalSection(&This->csFilter);
435 This->lpVtbl = NULL;
436 CoTaskMemFree(This->pszFileName);
437 if (This->pmt)
438 FreeMediaType(This->pmt);
439 CoTaskMemFree(This);
440 return 0;
442 else
443 return refCount;
446 /** IPersist methods **/
448 static HRESULT WINAPI AsyncReader_GetClassID(IBaseFilter * iface, CLSID * pClsid)
450 TRACE("(%p)\n", pClsid);
452 *pClsid = CLSID_AsyncReader;
454 return S_OK;
457 /** IMediaFilter methods **/
459 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
461 AsyncReader *This = (AsyncReader *)iface;
463 TRACE("()\n");
465 This->state = State_Stopped;
467 return S_OK;
470 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
472 AsyncReader *This = (AsyncReader *)iface;
474 TRACE("()\n");
476 This->state = State_Paused;
478 return S_OK;
481 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
483 AsyncReader *This = (AsyncReader *)iface;
485 TRACE("(%x%08x)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
487 This->state = State_Running;
489 return S_OK;
492 static HRESULT WINAPI AsyncReader_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
494 AsyncReader *This = (AsyncReader *)iface;
496 TRACE("(%u, %p)\n", dwMilliSecsTimeout, pState);
498 *pState = This->state;
500 return S_OK;
503 static HRESULT WINAPI AsyncReader_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
505 /* AsyncReader *This = (AsyncReader *)iface;*/
507 TRACE("(%p)\n", pClock);
509 return S_OK;
512 static HRESULT WINAPI AsyncReader_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
514 /* AsyncReader *This = (AsyncReader *)iface;*/
516 TRACE("(%p)\n", ppClock);
518 return S_OK;
521 /** IBaseFilter methods **/
523 static HRESULT WINAPI AsyncReader_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
525 ENUMPINDETAILS epd;
526 AsyncReader *This = (AsyncReader *)iface;
528 TRACE("(%p)\n", ppEnum);
530 epd.cPins = This->pOutputPin ? 1 : 0;
531 epd.ppPins = &This->pOutputPin;
532 return IEnumPinsImpl_Construct(&epd, ppEnum);
535 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
537 FIXME("(%s, %p)\n", debugstr_w(Id), ppPin);
539 return E_NOTIMPL;
542 static HRESULT WINAPI AsyncReader_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
544 AsyncReader *This = (AsyncReader *)iface;
546 TRACE("(%p)\n", pInfo);
548 strcpyW(pInfo->achName, This->filterInfo.achName);
549 pInfo->pGraph = This->filterInfo.pGraph;
551 if (pInfo->pGraph)
552 IFilterGraph_AddRef(pInfo->pGraph);
554 return S_OK;
557 static HRESULT WINAPI AsyncReader_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
559 AsyncReader *This = (AsyncReader *)iface;
561 TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
563 if (pName)
564 strcpyW(This->filterInfo.achName, pName);
565 else
566 *This->filterInfo.achName = 0;
567 This->filterInfo.pGraph = pGraph; /* NOTE: do NOT increase ref. count */
569 return S_OK;
572 static HRESULT WINAPI AsyncReader_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
574 FIXME("(%p)\n", pVendorInfo);
576 return E_NOTIMPL;
579 static const IBaseFilterVtbl AsyncReader_Vtbl =
581 AsyncReader_QueryInterface,
582 AsyncReader_AddRef,
583 AsyncReader_Release,
584 AsyncReader_GetClassID,
585 AsyncReader_Stop,
586 AsyncReader_Pause,
587 AsyncReader_Run,
588 AsyncReader_GetState,
589 AsyncReader_SetSyncSource,
590 AsyncReader_GetSyncSource,
591 AsyncReader_EnumPins,
592 AsyncReader_FindPin,
593 AsyncReader_QueryFilterInfo,
594 AsyncReader_JoinFilterGraph,
595 AsyncReader_QueryVendorInfo
598 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
600 AsyncReader *This = impl_from_IFileSourceFilter(iface);
602 return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->lpVtbl, riid, ppv);
605 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
607 AsyncReader *This = impl_from_IFileSourceFilter(iface);
609 return IBaseFilter_AddRef((IFileSourceFilter*)&This->lpVtbl);
612 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
614 AsyncReader *This = impl_from_IFileSourceFilter(iface);
616 return IBaseFilter_Release((IFileSourceFilter*)&This->lpVtbl);
619 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
621 HRESULT hr;
622 HANDLE hFile;
623 IAsyncReader * pReader = NULL;
624 AsyncReader *This = impl_from_IFileSourceFilter(iface);
626 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
628 /* open file */
629 /* FIXME: check the sharing values that native uses */
630 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
632 if (hFile == INVALID_HANDLE_VALUE)
634 return HRESULT_FROM_WIN32(GetLastError());
637 /* create pin */
638 hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->lpVtbl, &This->csFilter, &This->pOutputPin);
640 if (SUCCEEDED(hr))
641 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
643 /* store file name & media type */
644 if (SUCCEEDED(hr))
646 CoTaskMemFree(This->pszFileName);
647 if (This->pmt)
648 FreeMediaType(This->pmt);
650 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
651 strcpyW(This->pszFileName, pszFileName);
653 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
654 if (!pmt)
656 This->pmt->bFixedSizeSamples = TRUE;
657 This->pmt->bTemporalCompression = FALSE;
658 This->pmt->cbFormat = 0;
659 This->pmt->pbFormat = NULL;
660 This->pmt->pUnk = NULL;
661 This->pmt->lSampleSize = 0;
662 This->pmt->formattype = FORMAT_None;
663 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype);
664 if (FAILED(hr))
666 CoTaskMemFree(This->pmt);
667 This->pmt = NULL;
670 else
671 CopyMediaType(This->pmt, pmt);
674 if (pReader)
675 IAsyncReader_Release(pReader);
677 if (FAILED(hr))
679 if (This->pOutputPin)
681 IPin_Release(This->pOutputPin);
682 This->pOutputPin = NULL;
685 CoTaskMemFree(This->pszFileName);
686 if (This->pmt)
687 FreeMediaType(This->pmt);
688 This->pszFileName = NULL;
689 This->pmt = NULL;
691 CloseHandle(hFile);
694 /* FIXME: check return codes */
695 return hr;
698 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
700 AsyncReader *This = impl_from_IFileSourceFilter(iface);
702 TRACE("(%p, %p)\n", ppszFileName, pmt);
704 if (!ppszFileName)
705 return E_POINTER;
707 /* copy file name & media type if available, otherwise clear the outputs */
708 if (This->pszFileName)
710 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
711 strcpyW(*ppszFileName, This->pszFileName);
713 else
714 *ppszFileName = NULL;
716 if (pmt)
718 if (This->pmt)
719 CopyMediaType(pmt, This->pmt);
720 else
721 ZeroMemory(pmt, sizeof(*pmt));
724 return S_OK;
727 static const IFileSourceFilterVtbl FileSource_Vtbl =
729 FileSource_QueryInterface,
730 FileSource_AddRef,
731 FileSource_Release,
732 FileSource_Load,
733 FileSource_GetCurFile
737 /* the dwUserData passed back to user */
738 typedef struct DATAREQUEST
740 IMediaSample * pSample; /* sample passed to us by user */
741 DWORD_PTR dwUserData; /* user data passed to us */
742 OVERLAPPED ovl; /* our overlapped structure */
744 struct DATAREQUEST * pNext; /* next data request in list */
745 } DATAREQUEST;
747 static void queue(DATAREQUEST * pHead, DATAREQUEST * pItem)
749 DATAREQUEST * pCurrent;
750 for (pCurrent = pHead; pCurrent->pNext; pCurrent = pCurrent->pNext)
752 pCurrent->pNext = pItem;
755 typedef struct FileAsyncReader
757 OutputPin pin;
758 const struct IAsyncReaderVtbl * lpVtblAR;
760 HANDLE hFile;
761 HANDLE hEvent;
762 BOOL bFlushing;
763 DATAREQUEST * pHead; /* head of data request list */
764 CRITICAL_SECTION csList; /* critical section to protect operations on list */
765 } FileAsyncReader;
767 static inline FileAsyncReader *impl_from_IAsyncReader( IAsyncReader *iface )
769 return (FileAsyncReader *)((char*)iface - FIELD_OFFSET(FileAsyncReader, lpVtblAR));
772 static HRESULT AcceptProcAFR(LPVOID iface, const AM_MEDIA_TYPE *pmt)
774 AsyncReader *This = (AsyncReader *)iface;
776 FIXME("(%p, %p)\n", iface, pmt);
778 if (IsEqualGUID(&pmt->majortype, &This->pmt->majortype) &&
779 IsEqualGUID(&pmt->subtype, &This->pmt->subtype) &&
780 IsEqualGUID(&pmt->formattype, &FORMAT_None))
781 return S_OK;
783 return S_FALSE;
786 /* overridden pin functions */
788 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
790 FileAsyncReader *This = (FileAsyncReader *)iface;
791 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
793 *ppv = NULL;
795 if (IsEqualIID(riid, &IID_IUnknown))
796 *ppv = (LPVOID)This;
797 else if (IsEqualIID(riid, &IID_IPin))
798 *ppv = (LPVOID)This;
799 else if (IsEqualIID(riid, &IID_IAsyncReader))
800 *ppv = (LPVOID)&This->lpVtblAR;
802 if (*ppv)
804 IUnknown_AddRef((IUnknown *)(*ppv));
805 return S_OK;
808 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking))
809 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
811 return E_NOINTERFACE;
814 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
816 FileAsyncReader *This = (FileAsyncReader *)iface;
817 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
819 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
821 if (!refCount)
823 DATAREQUEST * pCurrent;
824 DATAREQUEST * pNext;
825 for (pCurrent = This->pHead; pCurrent; pCurrent = pNext)
827 pNext = pCurrent->pNext;
828 CoTaskMemFree(pCurrent);
830 CloseHandle(This->hFile);
831 CloseHandle(This->hEvent);
832 This->csList.DebugInfo->Spare[0] = 0;
833 DeleteCriticalSection(&This->csList);
834 CoTaskMemFree(This);
835 return 0;
837 return refCount;
840 static HRESULT WINAPI FileAsyncReaderPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
842 ENUMMEDIADETAILS emd;
843 FileAsyncReader *This = (FileAsyncReader *)iface;
845 TRACE("(%p)\n", ppEnum);
847 emd.cMediaTypes = 1;
848 emd.pMediaTypes = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
850 return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
853 static const IPinVtbl FileAsyncReaderPin_Vtbl =
855 FileAsyncReaderPin_QueryInterface,
856 IPinImpl_AddRef,
857 FileAsyncReaderPin_Release,
858 OutputPin_Connect,
859 OutputPin_ReceiveConnection,
860 IPinImpl_Disconnect,
861 IPinImpl_ConnectedTo,
862 IPinImpl_ConnectionMediaType,
863 IPinImpl_QueryPinInfo,
864 IPinImpl_QueryDirection,
865 IPinImpl_QueryId,
866 IPinImpl_QueryAccept,
867 FileAsyncReaderPin_EnumMediaTypes,
868 IPinImpl_QueryInternalConnections,
869 OutputPin_EndOfStream,
870 OutputPin_BeginFlush,
871 OutputPin_EndFlush,
872 OutputPin_NewSegment
875 /* Function called as a helper to IPin_Connect */
876 /* specific AM_MEDIA_TYPE - it cannot be NULL */
877 /* this differs from standard OutputPin_ConnectSpecific only in that it
878 * doesn't need the IMemInputPin interface on the receiving pin */
879 static HRESULT FileAsyncReaderPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
881 OutputPin *This = (OutputPin *)iface;
882 HRESULT hr;
884 TRACE("(%p, %p)\n", pReceivePin, pmt);
885 dump_AM_MEDIA_TYPE(pmt);
887 /* FIXME: call queryacceptproc */
889 This->pin.pConnectedTo = pReceivePin;
890 IPin_AddRef(pReceivePin);
891 CopyMediaType(&This->pin.mtCurrent, pmt);
893 hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
895 if (FAILED(hr))
897 IPin_Release(This->pin.pConnectedTo);
898 This->pin.pConnectedTo = NULL;
899 FreeMediaType(&This->pin.mtCurrent);
902 TRACE(" -- %x\n", hr);
903 return hr;
906 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
908 PIN_INFO piOutput;
909 HRESULT hr;
911 *ppPin = NULL;
912 piOutput.dir = PINDIR_OUTPUT;
913 piOutput.pFilter = pBaseFilter;
914 strcpyW(piOutput.achName, wszOutputPinName);
915 hr = OutputPin_Construct(&FileAsyncReaderPin_Vtbl, sizeof(FileAsyncReader), &piOutput, NULL, pBaseFilter, AcceptProcAFR, pCritSec, ppPin);
917 if (SUCCEEDED(hr))
919 FileAsyncReader *pPinImpl = (FileAsyncReader *)*ppPin;
920 pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
921 pPinImpl->hFile = hFile;
922 pPinImpl->hEvent = CreateEventW(NULL, 0, 0, NULL);
923 pPinImpl->bFlushing = FALSE;
924 pPinImpl->pHead = NULL;
925 pPinImpl->pin.pConnectSpecific = FileAsyncReaderPin_ConnectSpecific;
926 InitializeCriticalSection(&pPinImpl->csList);
927 pPinImpl->csList.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FileAsyncReader.csList");
929 return hr;
932 /* IAsyncReader */
934 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
936 FileAsyncReader *This = impl_from_IAsyncReader(iface);
938 return IPin_QueryInterface((IPin *)This, riid, ppv);
941 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
943 FileAsyncReader *This = impl_from_IAsyncReader(iface);
945 return IPin_AddRef((IPin *)This);
948 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
950 FileAsyncReader *This = impl_from_IAsyncReader(iface);
952 return IPin_Release((IPin *)This);
955 #define DEF_ALIGNMENT 1
957 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
959 HRESULT hr = S_OK;
961 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
963 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
964 pProps->cbAlign = DEF_ALIGNMENT;
966 if (pPreferred)
968 ALLOCATOR_PROPERTIES PropsActual;
969 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
970 /* FIXME: check we are still aligned */
971 if (SUCCEEDED(hr))
973 IMemAllocator_AddRef(pPreferred);
974 *ppActual = pPreferred;
975 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
976 return S_OK;
980 pPreferred = NULL;
982 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
984 if (SUCCEEDED(hr))
986 ALLOCATOR_PROPERTIES PropsActual;
987 hr = IMemAllocator_SetProperties(pPreferred, pProps, &PropsActual);
988 /* FIXME: check we are still aligned */
989 if (SUCCEEDED(hr))
991 *ppActual = pPreferred;
992 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
993 return S_OK;
997 if (FAILED(hr))
999 *ppActual = NULL;
1000 if (pPreferred)
1001 IMemAllocator_Release(pPreferred);
1004 TRACE("-- %x\n", hr);
1005 return hr;
1008 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
1009 * however, this would be quite complicated to do and may be a bit error prone */
1010 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
1012 REFERENCE_TIME Start;
1013 REFERENCE_TIME Stop;
1014 DATAREQUEST * pDataRq;
1015 BYTE * pBuffer;
1016 HRESULT hr = S_OK;
1017 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1019 TRACE("(%p, %lx)\n", pSample, dwUser);
1021 /* check flushing state */
1022 if (This->bFlushing)
1023 return VFW_E_WRONG_STATE;
1025 if (!(pDataRq = CoTaskMemAlloc(sizeof(*pDataRq))))
1026 hr = E_OUTOFMEMORY;
1028 /* get start and stop positions in bytes */
1029 if (SUCCEEDED(hr))
1030 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
1032 if (SUCCEEDED(hr))
1033 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1035 if (SUCCEEDED(hr))
1037 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
1039 pDataRq->ovl.u.s.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
1040 pDataRq->ovl.u.s.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
1041 pDataRq->ovl.hEvent = This->hEvent;
1042 pDataRq->dwUserData = dwUser;
1043 pDataRq->pNext = NULL;
1044 /* we violate traditional COM rules here by maintaining
1045 * a reference to the sample, but not calling AddRef, but
1046 * that's what MSDN says to do */
1047 pDataRq->pSample = pSample;
1049 EnterCriticalSection(&This->csList);
1051 if (This->pHead)
1052 /* adds data request to end of list */
1053 queue(This->pHead, pDataRq);
1054 else
1055 This->pHead = pDataRq;
1057 LeaveCriticalSection(&This->csList);
1059 /* this is definitely not how it is implemented on Win9x
1060 * as they do not support async reads on files, but it is
1061 * sooo much easier to use this than messing around with threads!
1063 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1064 hr = HRESULT_FROM_WIN32(GetLastError());
1066 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1067 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1068 hr = S_OK;
1071 if (FAILED(hr) && pDataRq)
1073 EnterCriticalSection(&This->csList);
1075 DATAREQUEST * pCurrent;
1076 for (pCurrent = This->pHead; pCurrent && pCurrent->pNext; pCurrent = pCurrent->pNext)
1077 if (pCurrent->pNext == pDataRq)
1079 pCurrent->pNext = pDataRq->pNext;
1080 break;
1083 LeaveCriticalSection(&This->csList);
1084 CoTaskMemFree(pDataRq);
1087 TRACE("-- %x\n", hr);
1088 return hr;
1091 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1093 HRESULT hr = S_OK;
1094 DWORD dwBytes = 0;
1095 DATAREQUEST * pDataRq = NULL;
1096 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1098 TRACE("(%u, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1100 /* FIXME: we could do with improving this by waiting for an array of event handles
1101 * and then determining which one finished and removing that from the list, otherwise
1102 * we will end up waiting for longer than we should do, if a later request finishes
1103 * before an earlier one */
1105 *ppSample = NULL;
1106 *pdwUser = 0;
1108 if (!This->bFlushing)
1110 /* wait for the read to finish or timeout */
1111 if (WaitForSingleObject(This->hEvent, dwTimeout) == WAIT_TIMEOUT)
1112 hr = VFW_E_TIMEOUT;
1115 if (SUCCEEDED(hr))
1117 EnterCriticalSection(&This->csList);
1119 pDataRq = This->pHead;
1120 if (pDataRq == NULL)
1121 hr = E_FAIL;
1122 else
1123 This->pHead = pDataRq->pNext;
1125 LeaveCriticalSection(&This->csList);
1128 if (SUCCEEDED(hr) && !This->bFlushing)
1130 /* get any errors */
1131 if (!GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1132 hr = HRESULT_FROM_WIN32(GetLastError());
1135 if (SUCCEEDED(hr))
1137 IMediaSample_SetActualDataLength(pDataRq->pSample, dwBytes);
1138 *ppSample = pDataRq->pSample;
1139 *pdwUser = pDataRq->dwUserData;
1142 /* no need to close event handle since we will close it when the pin is destroyed */
1143 CoTaskMemFree(pDataRq);
1145 /* Return the sample if flushing so it can be destroyed */
1146 if (This->bFlushing && SUCCEEDED(hr))
1148 hr = VFW_E_WRONG_STATE;
1149 IMediaSample_SetActualDataLength(pDataRq->pSample, 0);
1152 TRACE("-- %x\n", hr);
1153 return hr;
1156 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1158 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1160 BYTE * pBuffer;
1161 REFERENCE_TIME tStart;
1162 REFERENCE_TIME tStop;
1163 HRESULT hr;
1165 TRACE("(%p)\n", pSample);
1167 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1169 if (SUCCEEDED(hr))
1170 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1172 if (SUCCEEDED(hr))
1173 hr = FileAsyncReader_SyncRead(iface,
1174 BYTES_FROM_MEDIATIME(tStart),
1175 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1176 pBuffer);
1178 TRACE("-- %x\n", hr);
1179 return hr;
1182 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1184 OVERLAPPED ovl;
1185 HRESULT hr = S_OK;
1186 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1188 TRACE("(%x%08x, %d, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1190 ZeroMemory(&ovl, sizeof(ovl));
1192 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1193 /* NOTE: llPosition is the actual byte position to start reading from */
1194 ovl.u.s.Offset = (DWORD) llPosition;
1195 ovl.u.s.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1197 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1198 hr = HRESULT_FROM_WIN32(GetLastError());
1200 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1201 hr = S_OK;
1203 if (SUCCEEDED(hr))
1205 DWORD dwBytesRead;
1207 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1208 hr = HRESULT_FROM_WIN32(GetLastError());
1211 CloseHandle(ovl.hEvent);
1213 TRACE("-- %x\n", hr);
1214 return hr;
1217 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1219 DWORD dwSizeLow;
1220 DWORD dwSizeHigh;
1221 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1223 TRACE("(%p, %p)\n", pTotal, pAvailable);
1225 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1226 (GetLastError() != NO_ERROR))
1227 return HRESULT_FROM_WIN32(GetLastError());
1229 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1231 *pAvailable = *pTotal;
1233 return S_OK;
1236 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1238 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1240 TRACE("()\n");
1242 This->bFlushing = TRUE;
1243 CancelIo(This->hFile);
1244 SetEvent(This->hEvent);
1246 /* FIXME: free list */
1248 return S_OK;
1251 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1253 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1255 TRACE("()\n");
1257 This->bFlushing = FALSE;
1259 return S_OK;
1262 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1264 FileAsyncReader_QueryInterface,
1265 FileAsyncReader_AddRef,
1266 FileAsyncReader_Release,
1267 FileAsyncReader_RequestAllocator,
1268 FileAsyncReader_Request,
1269 FileAsyncReader_WaitForNext,
1270 FileAsyncReader_SyncReadAligned,
1271 FileAsyncReader_SyncRead,
1272 FileAsyncReader_Length,
1273 FileAsyncReader_BeginFlush,
1274 FileAsyncReader_EndFlush,