quartz: Make the file source support multiple samples by preallocating enough handles.
[wine] / dlls / quartz / filesource.c
1 /*
2  * File Source Filter
3  *
4  * Copyright 2003 Robert Shearman
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23
24 #include "quartz_private.h"
25
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>
35
36 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
37
38 static const WCHAR wszOutputPinName[] = { 'O','u','t','p','u','t',0 };
39
40 typedef struct AsyncReader
41 {
42     const IBaseFilterVtbl * lpVtbl;
43     const IFileSourceFilterVtbl * lpVtblFSF;
44
45     LONG refCount;
46     FILTER_INFO filterInfo;
47     FILTER_STATE state;
48     CRITICAL_SECTION csFilter;
49
50     IPin * pOutputPin;
51     LPOLESTR pszFileName;
52     AM_MEDIA_TYPE * pmt;
53 } AsyncReader;
54
55 static const IBaseFilterVtbl AsyncReader_Vtbl;
56 static const IFileSourceFilterVtbl FileSource_Vtbl;
57 static const IAsyncReaderVtbl FileAsyncReader_Vtbl;
58
59 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
60
61 static inline AsyncReader *impl_from_IFileSourceFilter( IFileSourceFilter *iface )
62 {
63     return (AsyncReader *)((char*)iface - FIELD_OFFSET(AsyncReader, lpVtblFSF));
64 }
65
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 };
70
71 static HRESULT process_extensions(HKEY hkeyExtensions, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
72 {
73     WCHAR *extension;
74     LONG l;
75     HKEY hsub;
76     WCHAR keying[39];
77     DWORD size;
78
79     if (!pszFileName)
80         return E_POINTER;
81
82     /* Get the part of the name that matters */
83     extension = PathFindExtensionW(pszFileName);
84     if (*extension != '.')
85         return E_FAIL;
86
87     l = RegOpenKeyExW(hkeyExtensions, extension, 0, KEY_READ, &hsub);
88     if (l)
89         return E_FAIL;
90
91     size = sizeof(keying);
92     l = RegQueryValueExW(hsub, mediatype_name, NULL, NULL, (LPBYTE)keying, &size);
93     if (!l)
94         CLSIDFromString(keying, majorType);
95
96     size = sizeof(keying);
97     if (!l)
98         l = RegQueryValueExW(hsub, subtype_name, NULL, NULL, (LPBYTE)keying, &size);
99
100     if (!l)
101         CLSIDFromString(keying, minorType);
102
103     RegCloseKey(hsub);
104
105     if (!l)
106         return S_OK;
107     return E_FAIL;
108 }
109
110 static unsigned char byte_from_hex_char(WCHAR wHex)
111 {
112     switch (tolowerW(wHex))
113     {
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;
134     }
135 }
136
137 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
138 {
139     ULONG ulOffset;
140     ULONG ulBytes;
141     BYTE * pbMask;
142     BYTE * pbValue;
143     BYTE * pbFile;
144     HRESULT hr = S_OK;
145     ULONG strpos;
146
147     TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
148     
149     /* format: "offset, bytestocompare, mask, value" */
150
151     ulOffset = strtolW(wszPatternString, NULL, 10);
152
153     if (!(wszPatternString = strchrW(wszPatternString, ',')))
154         return E_INVALIDARG;
155
156     wszPatternString++; /* skip ',' */
157
158     ulBytes = strtolW(wszPatternString, NULL, 10);
159
160     pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
161     pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
162     pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
163
164     /* default mask is match everything */
165     memset(pbMask, 0xFF, ulBytes);
166
167     if (!(wszPatternString = strchrW(wszPatternString, ',')))
168         hr = E_INVALIDARG;
169
170     wszPatternString++; /* skip ',' */
171
172     if (hr == S_OK)
173     {
174         for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
175             ;
176
177         for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
178         {
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;
183         }
184
185         if (!(wszPatternString = strchrW(wszPatternString, ',')))
186             hr = E_INVALIDARG;
187     
188         wszPatternString++; /* skip ',' */
189     }
190
191     if (hr == S_OK)
192     {
193         for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
194             ;
195
196         for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
197         {
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;
202         }
203     }
204
205     if (hr == S_OK)
206         hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
207
208     if (hr == S_OK)
209     {
210         ULONG i;
211         for (i = 0; i < ulBytes; i++)
212             if ((pbFile[i] & pbMask[i]) != pbValue[i])
213             {
214                 hr = S_FALSE;
215                 break;
216             }
217     }
218
219     HeapFree(GetProcessHeap(), 0, pbMask);
220     HeapFree(GetProcessHeap(), 0, pbValue);
221     HeapFree(GetProcessHeap(), 0, pbFile);
222
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;
229 }
230
231 static HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType)
232 {
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};
238
239     TRACE("(%p, %s, %p, %p)\n", pReader, debugstr_w(pszFileName), majorType, minorType);
240
241     *majorType = GUID_NULL;
242     *minorType = GUID_NULL;
243
244     lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType);
245     hr = HRESULT_FROM_WIN32(lRet);
246
247     if (SUCCEEDED(hr))
248     {
249         DWORD indexMajor;
250
251         for (indexMajor = 0; !bFound; indexMajor++)
252         {
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};
257
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))
264             {
265                 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType) == S_OK)
266                     bFound = TRUE;
267             }
268             else
269             {
270                 DWORD indexMinor;
271
272                 for (indexMinor = 0; !bFound; indexMinor++)
273                 {
274                     HKEY hkeyMinor;
275                     WCHAR wszMinorKeyName[CHARS_IN_GUID];
276                     DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
277                     DWORD maxValueLen;
278                     DWORD indexValue;
279
280                     if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
281                         break;
282
283                     if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
284                         break;
285
286                     TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
287         
288                     if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
289                         break;
290
291                     for (indexValue = 0; !bFound; indexValue++)
292                     {
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;
300
301                         if ((temp = RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen)) != ERROR_SUCCESS)
302                         {
303                             HeapFree(GetProcessHeap(), 0, wszPatternString);
304                             break;
305                         }
306
307                         /* if it is not the source filter value */
308                         if (strcmpW(wszValueName, wszSourceFilter))
309                         {
310                             if (process_pattern_string(wszPatternString, pReader) == S_OK)
311                             {
312                                 if (SUCCEEDED(CLSIDFromString(wszMajorKeyName, majorType)) &&
313                                     SUCCEEDED(CLSIDFromString(wszMinorKeyName, minorType)))
314                                     bFound = TRUE;
315                             }
316                         }
317                         HeapFree(GetProcessHeap(), 0, wszPatternString);
318                     }
319                     CloseHandle(hkeyMinor);
320                 }
321             }
322             CloseHandle(hkeyMajor);
323         }
324     }
325     CloseHandle(hkeyMediaType);
326
327     if (SUCCEEDED(hr) && !bFound)
328     {
329         ERR("Media class not found\n");
330         hr = E_FAIL;
331     }
332     else if (bFound)
333         TRACE("Found file's class: major = %s, subtype = %s\n", qzdebugstr_guid(majorType), qzdebugstr_guid(minorType));
334
335     return hr;
336 }
337
338 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
339 {
340     AsyncReader *pAsyncRead;
341     
342     if( pUnkOuter )
343         return CLASS_E_NOAGGREGATION;
344     
345     pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
346
347     if (!pAsyncRead)
348         return E_OUTOFMEMORY;
349
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;
356
357     InitializeCriticalSection(&pAsyncRead->csFilter);
358     pAsyncRead->csFilter.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": AsyncReader.csFilter");
359
360     pAsyncRead->pszFileName = NULL;
361     pAsyncRead->pmt = NULL;
362
363     *ppv = (LPVOID)pAsyncRead;
364
365     TRACE("-- created at %p\n", pAsyncRead);
366
367     return S_OK;
368 }
369
370 /** IUnknown methods **/
371
372 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
373 {
374     AsyncReader *This = (AsyncReader *)iface;
375
376     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
377
378     *ppv = NULL;
379
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);
390
391     if (*ppv)
392     {
393         IUnknown_AddRef((IUnknown *)(*ppv));
394         return S_OK;
395     }
396
397     if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking) && !IsEqualIID(riid, &IID_IVideoWindow))
398         FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
399
400     return E_NOINTERFACE;
401 }
402
403 static ULONG WINAPI AsyncReader_AddRef(IBaseFilter * iface)
404 {
405     AsyncReader *This = (AsyncReader *)iface;
406     ULONG refCount = InterlockedIncrement(&This->refCount);
407     
408     TRACE("(%p)->() AddRef from %d\n", This, refCount - 1);
409     
410     return refCount;
411 }
412
413 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
414 {
415     AsyncReader *This = (AsyncReader *)iface;
416     ULONG refCount = InterlockedDecrement(&This->refCount);
417     
418     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
419     
420     if (!refCount)
421     {
422         if (This->pOutputPin)
423         {
424             IPin *pConnectedTo;
425             if(SUCCEEDED(IPin_ConnectedTo(This->pOutputPin, &pConnectedTo)))
426             {
427                 IPin_Disconnect(pConnectedTo);
428                 IPin_Release(pConnectedTo);
429             }
430             IPin_Disconnect(This->pOutputPin);
431             IPin_Release(This->pOutputPin);
432         }
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;
441     }
442     else
443         return refCount;
444 }
445
446 /** IPersist methods **/
447
448 static HRESULT WINAPI AsyncReader_GetClassID(IBaseFilter * iface, CLSID * pClsid)
449 {
450     TRACE("(%p)\n", pClsid);
451
452     *pClsid = CLSID_AsyncReader;
453
454     return S_OK;
455 }
456
457 /** IMediaFilter methods **/
458
459 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
460 {
461     AsyncReader *This = (AsyncReader *)iface;
462
463     TRACE("()\n");
464
465     This->state = State_Stopped;
466     
467     return S_OK;
468 }
469
470 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
471 {
472     AsyncReader *This = (AsyncReader *)iface;
473
474     TRACE("()\n");
475
476     This->state = State_Paused;
477
478     return S_OK;
479 }
480
481 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
482 {
483     AsyncReader *This = (AsyncReader *)iface;
484
485     TRACE("(%x%08x)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
486
487     This->state = State_Running;
488
489     return S_OK;
490 }
491
492 static HRESULT WINAPI AsyncReader_GetState(IBaseFilter * iface, DWORD dwMilliSecsTimeout, FILTER_STATE *pState)
493 {
494     AsyncReader *This = (AsyncReader *)iface;
495
496     TRACE("(%u, %p)\n", dwMilliSecsTimeout, pState);
497
498     *pState = This->state;
499     
500     return S_OK;
501 }
502
503 static HRESULT WINAPI AsyncReader_SetSyncSource(IBaseFilter * iface, IReferenceClock *pClock)
504 {
505 /*    AsyncReader *This = (AsyncReader *)iface;*/
506
507     TRACE("(%p)\n", pClock);
508
509     return S_OK;
510 }
511
512 static HRESULT WINAPI AsyncReader_GetSyncSource(IBaseFilter * iface, IReferenceClock **ppClock)
513 {
514 /*    AsyncReader *This = (AsyncReader *)iface;*/
515
516     TRACE("(%p)\n", ppClock);
517
518     return S_OK;
519 }
520
521 /** IBaseFilter methods **/
522
523 static HRESULT WINAPI AsyncReader_EnumPins(IBaseFilter * iface, IEnumPins **ppEnum)
524 {
525     ENUMPINDETAILS epd;
526     AsyncReader *This = (AsyncReader *)iface;
527
528     TRACE("(%p)\n", ppEnum);
529
530     epd.cPins = This->pOutputPin ? 1 : 0;
531     epd.ppPins = &This->pOutputPin;
532     return IEnumPinsImpl_Construct(&epd, ppEnum);
533 }
534
535 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
536 {
537     FIXME("(%s, %p)\n", debugstr_w(Id), ppPin);
538
539     return E_NOTIMPL;
540 }
541
542 static HRESULT WINAPI AsyncReader_QueryFilterInfo(IBaseFilter * iface, FILTER_INFO *pInfo)
543 {
544     AsyncReader *This = (AsyncReader *)iface;
545
546     TRACE("(%p)\n", pInfo);
547
548     strcpyW(pInfo->achName, This->filterInfo.achName);
549     pInfo->pGraph = This->filterInfo.pGraph;
550
551     if (pInfo->pGraph)
552         IFilterGraph_AddRef(pInfo->pGraph);
553     
554     return S_OK;
555 }
556
557 static HRESULT WINAPI AsyncReader_JoinFilterGraph(IBaseFilter * iface, IFilterGraph *pGraph, LPCWSTR pName)
558 {
559     AsyncReader *This = (AsyncReader *)iface;
560
561     TRACE("(%p, %s)\n", pGraph, debugstr_w(pName));
562
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 */
568
569     return S_OK;
570 }
571
572 static HRESULT WINAPI AsyncReader_QueryVendorInfo(IBaseFilter * iface, LPWSTR *pVendorInfo)
573 {
574     FIXME("(%p)\n", pVendorInfo);
575
576     return E_NOTIMPL;
577 }
578
579 static const IBaseFilterVtbl AsyncReader_Vtbl =
580 {
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
596 };
597
598 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
599 {
600     AsyncReader *This = impl_from_IFileSourceFilter(iface);
601
602     return IBaseFilter_QueryInterface((IFileSourceFilter*)&This->lpVtbl, riid, ppv);
603 }
604
605 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
606 {
607     AsyncReader *This = impl_from_IFileSourceFilter(iface);
608
609     return IBaseFilter_AddRef((IFileSourceFilter*)&This->lpVtbl);
610 }
611
612 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
613 {
614     AsyncReader *This = impl_from_IFileSourceFilter(iface);
615
616     return IBaseFilter_Release((IFileSourceFilter*)&This->lpVtbl);
617 }
618
619 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
620 {
621     HRESULT hr;
622     HANDLE hFile;
623     IAsyncReader * pReader = NULL;
624     AsyncReader *This = impl_from_IFileSourceFilter(iface);
625
626     TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
627
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);
631
632     if (hFile == INVALID_HANDLE_VALUE)
633     {
634         return HRESULT_FROM_WIN32(GetLastError());
635     }
636
637     /* create pin */
638     hr = FileAsyncReader_Construct(hFile, (IBaseFilter *)&This->lpVtbl, &This->csFilter, &This->pOutputPin);
639
640     if (SUCCEEDED(hr))
641         hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
642
643     /* store file name & media type */
644     if (SUCCEEDED(hr))
645     {
646         CoTaskMemFree(This->pszFileName);
647         if (This->pmt)
648             FreeMediaType(This->pmt);
649
650         This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
651         strcpyW(This->pszFileName, pszFileName);
652
653         This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
654         if (!pmt)
655         {
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))
665             {
666                 CoTaskMemFree(This->pmt);
667                 This->pmt = NULL;
668             }
669         }
670         else
671             CopyMediaType(This->pmt, pmt);
672     }
673
674     if (pReader)
675         IAsyncReader_Release(pReader);
676
677     if (FAILED(hr))
678     {
679         if (This->pOutputPin)
680         {
681             IPin_Release(This->pOutputPin);
682             This->pOutputPin = NULL;
683         }
684
685         CoTaskMemFree(This->pszFileName);
686         if (This->pmt)
687             FreeMediaType(This->pmt);
688         This->pszFileName = NULL;
689         This->pmt = NULL;
690
691         CloseHandle(hFile);
692     }
693
694     /* FIXME: check return codes */
695     return hr;
696 }
697
698 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
699 {
700     AsyncReader *This = impl_from_IFileSourceFilter(iface);
701     
702     TRACE("(%p, %p)\n", ppszFileName, pmt);
703
704     if (!ppszFileName)
705         return E_POINTER;
706
707     /* copy file name & media type if available, otherwise clear the outputs */
708     if (This->pszFileName)
709     {
710         *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
711         strcpyW(*ppszFileName, This->pszFileName);
712     }
713     else
714         *ppszFileName = NULL;
715
716     if (pmt)
717     {
718         if (This->pmt)
719             CopyMediaType(pmt, This->pmt);
720         else
721             ZeroMemory(pmt, sizeof(*pmt));
722     }
723
724     return S_OK;
725 }
726
727 static const IFileSourceFilterVtbl FileSource_Vtbl = 
728 {
729     FileSource_QueryInterface,
730     FileSource_AddRef,
731     FileSource_Release,
732     FileSource_Load,
733     FileSource_GetCurFile
734 };
735
736
737 /* the dwUserData passed back to user */
738 typedef struct DATAREQUEST
739 {
740     IMediaSample * pSample; /* sample passed to us by user */
741     DWORD_PTR dwUserData; /* user data passed to us */
742     OVERLAPPED ovl; /* our overlapped structure */
743 } DATAREQUEST;
744
745 typedef struct FileAsyncReader
746 {
747     OutputPin pin;
748     const struct IAsyncReaderVtbl * lpVtblAR;
749
750     HANDLE hFile;
751     BOOL bFlushing;
752     /* Why would you need more? Every sample has its own handle */
753     LONG queued_number;
754     LONG samples;
755     CRITICAL_SECTION csList; /* critical section to prevent concurrency issues */
756     DATAREQUEST *sample_list;
757     HANDLE *handle_list;
758 } FileAsyncReader;
759
760 static inline FileAsyncReader *impl_from_IAsyncReader( IAsyncReader *iface )
761 {
762     return (FileAsyncReader *)((char*)iface - FIELD_OFFSET(FileAsyncReader, lpVtblAR));
763 }
764
765 static HRESULT AcceptProcAFR(LPVOID iface, const AM_MEDIA_TYPE *pmt)
766 {
767     AsyncReader *This = (AsyncReader *)iface;
768     
769     FIXME("(%p, %p)\n", iface, pmt);
770
771     if (IsEqualGUID(&pmt->majortype, &This->pmt->majortype) &&
772         IsEqualGUID(&pmt->subtype, &This->pmt->subtype) &&
773         IsEqualGUID(&pmt->formattype, &FORMAT_None))
774         return S_OK;
775     
776     return S_FALSE;
777 }
778
779 /* overridden pin functions */
780
781 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
782 {
783     FileAsyncReader *This = (FileAsyncReader *)iface;
784     TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
785
786     *ppv = NULL;
787
788     if (IsEqualIID(riid, &IID_IUnknown))
789         *ppv = (LPVOID)This;
790     else if (IsEqualIID(riid, &IID_IPin))
791         *ppv = (LPVOID)This;
792     else if (IsEqualIID(riid, &IID_IAsyncReader))
793         *ppv = (LPVOID)&This->lpVtblAR;
794
795     if (*ppv)
796     {
797         IUnknown_AddRef((IUnknown *)(*ppv));
798         return S_OK;
799     }
800
801     if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking))
802         FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
803
804     return E_NOINTERFACE;
805 }
806
807 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
808 {
809     FileAsyncReader *This = (FileAsyncReader *)iface;
810     ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
811     int x;
812
813     TRACE("(%p)->() Release from %d\n", This, refCount + 1);
814
815     if (!refCount)
816     {
817         CoTaskMemFree(This->sample_list);
818         if (This->handle_list)
819         {
820             for (x = 0; x < This->samples; ++x)
821                 CloseHandle(This->handle_list[x]);
822             CoTaskMemFree(This->handle_list);
823         }
824         CloseHandle(This->hFile);
825         This->csList.DebugInfo->Spare[0] = 0;
826         DeleteCriticalSection(&This->csList);
827         CoTaskMemFree(This);
828         return 0;
829     }
830     return refCount;
831 }
832
833 static HRESULT WINAPI FileAsyncReaderPin_EnumMediaTypes(IPin * iface, IEnumMediaTypes ** ppEnum)
834 {
835     ENUMMEDIADETAILS emd;
836     FileAsyncReader *This = (FileAsyncReader *)iface;
837
838     TRACE("(%p)\n", ppEnum);
839
840     emd.cMediaTypes = 1;
841     emd.pMediaTypes = ((AsyncReader *)This->pin.pin.pinInfo.pFilter)->pmt;
842
843     return IEnumMediaTypesImpl_Construct(&emd, ppEnum);
844 }
845
846 static const IPinVtbl FileAsyncReaderPin_Vtbl = 
847 {
848     FileAsyncReaderPin_QueryInterface,
849     IPinImpl_AddRef,
850     FileAsyncReaderPin_Release,
851     OutputPin_Connect,
852     OutputPin_ReceiveConnection,
853     IPinImpl_Disconnect,
854     IPinImpl_ConnectedTo,
855     IPinImpl_ConnectionMediaType,
856     IPinImpl_QueryPinInfo,
857     IPinImpl_QueryDirection,
858     IPinImpl_QueryId,
859     IPinImpl_QueryAccept,
860     FileAsyncReaderPin_EnumMediaTypes,
861     IPinImpl_QueryInternalConnections,
862     OutputPin_EndOfStream,
863     OutputPin_BeginFlush,
864     OutputPin_EndFlush,
865     OutputPin_NewSegment
866 };
867
868 /* Function called as a helper to IPin_Connect */
869 /* specific AM_MEDIA_TYPE - it cannot be NULL */
870 /* this differs from standard OutputPin_ConnectSpecific only in that it
871  * doesn't need the IMemInputPin interface on the receiving pin */
872 static HRESULT FileAsyncReaderPin_ConnectSpecific(IPin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
873 {
874     OutputPin *This = (OutputPin *)iface;
875     HRESULT hr;
876
877     TRACE("(%p, %p)\n", pReceivePin, pmt);
878     dump_AM_MEDIA_TYPE(pmt);
879
880     /* FIXME: call queryacceptproc */
881
882     This->pin.pConnectedTo = pReceivePin;
883     IPin_AddRef(pReceivePin);
884     CopyMediaType(&This->pin.mtCurrent, pmt);
885
886     hr = IPin_ReceiveConnection(pReceivePin, iface, pmt);
887
888     if (FAILED(hr))
889     {
890         IPin_Release(This->pin.pConnectedTo);
891         This->pin.pConnectedTo = NULL;
892         FreeMediaType(&This->pin.mtCurrent);
893     }
894
895     TRACE(" -- %x\n", hr);
896     return hr;
897 }
898
899 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
900 {
901     PIN_INFO piOutput;
902     HRESULT hr;
903
904     *ppPin = NULL;
905     piOutput.dir = PINDIR_OUTPUT;
906     piOutput.pFilter = pBaseFilter;
907     strcpyW(piOutput.achName, wszOutputPinName);
908     hr = OutputPin_Construct(&FileAsyncReaderPin_Vtbl, sizeof(FileAsyncReader), &piOutput, NULL, pBaseFilter, AcceptProcAFR, pCritSec, ppPin);
909
910     if (SUCCEEDED(hr))
911     {
912         FileAsyncReader *pPinImpl =  (FileAsyncReader *)*ppPin;
913         pPinImpl->lpVtblAR = &FileAsyncReader_Vtbl;
914         pPinImpl->hFile = hFile;
915         pPinImpl->bFlushing = FALSE;
916         pPinImpl->sample_list = NULL;
917         pPinImpl->handle_list = NULL;
918         pPinImpl->queued_number = 0;
919         pPinImpl->pin.pConnectSpecific = FileAsyncReaderPin_ConnectSpecific;
920         InitializeCriticalSection(&pPinImpl->csList);
921         pPinImpl->csList.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FileAsyncReader.csList");
922     }
923     return hr;
924 }
925
926 /* IAsyncReader */
927
928 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
929 {
930     FileAsyncReader *This = impl_from_IAsyncReader(iface);
931
932     return IPin_QueryInterface((IPin *)This, riid, ppv);
933 }
934
935 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
936 {
937     FileAsyncReader *This = impl_from_IAsyncReader(iface);
938
939     return IPin_AddRef((IPin *)This);
940 }
941
942 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
943 {
944     FileAsyncReader *This = impl_from_IAsyncReader(iface);
945
946     return IPin_Release((IPin *)This);
947 }
948
949 #define DEF_ALIGNMENT 1
950
951 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
952 {
953     FileAsyncReader *This = impl_from_IAsyncReader(iface);
954
955     HRESULT hr = S_OK;
956
957     TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
958
959     if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
960         pProps->cbAlign = DEF_ALIGNMENT;
961
962     if (pPreferred)
963     {
964         hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
965         /* FIXME: check we are still aligned */
966         if (SUCCEEDED(hr))
967         {
968             IMemAllocator_AddRef(pPreferred);
969             *ppActual = pPreferred;
970             TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
971             goto done;
972         }
973     }
974
975     pPreferred = NULL;
976
977     hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
978
979     if (SUCCEEDED(hr))
980     {
981         hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
982         /* FIXME: check we are still aligned */
983         if (SUCCEEDED(hr))
984         {
985             *ppActual = pPreferred;
986             TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
987         }
988     }
989
990 done:
991     if (SUCCEEDED(hr))
992     {
993         CoTaskMemFree(This->sample_list);
994         if (This->handle_list)
995         {
996             int x;
997             for (x = 0; x < This->samples; ++x)
998                 CloseHandle(This->handle_list[x]);
999             CoTaskMemFree(This->handle_list);
1000         }
1001
1002         This->samples = pProps->cBuffers;
1003         TRACE("Samples: %u\n", This->samples);
1004         This->sample_list = CoTaskMemAlloc(sizeof(This->sample_list[0]) * pProps->cBuffers);
1005         This->handle_list = CoTaskMemAlloc(sizeof(HANDLE) * pProps->cBuffers);
1006
1007         if (This->sample_list && This->handle_list)
1008         {
1009             int x;
1010             ZeroMemory(This->sample_list, sizeof(This->sample_list[0]) * pProps->cBuffers);
1011             for (x = 0; x < This->samples; ++x)
1012             {
1013                 This->sample_list[x].ovl.hEvent = This->handle_list[x] = CreateEventW(NULL, 0, 0, NULL);
1014             }
1015             This->pin.allocProps = *pProps;
1016         }
1017         else
1018         {
1019             hr = E_OUTOFMEMORY;
1020             CoTaskMemFree(This->sample_list);
1021             CoTaskMemFree(This->handle_list);
1022             This->samples = 0;
1023             This->sample_list = NULL;
1024             This->handle_list = NULL;
1025         }
1026     }
1027
1028     if (FAILED(hr))
1029     {
1030         *ppActual = NULL;
1031         if (pPreferred)
1032             IMemAllocator_Release(pPreferred);
1033     }
1034
1035     TRACE("-- %x\n", hr);
1036     return hr;
1037 }
1038
1039 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
1040  * however, this would be quite complicated to do and may be a bit error prone */
1041 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
1042 {
1043     HRESULT hr = S_OK;
1044     REFERENCE_TIME Start;
1045     REFERENCE_TIME Stop;
1046     FileAsyncReader *This = impl_from_IAsyncReader(iface);
1047     LPBYTE pBuffer = NULL;
1048
1049     TRACE("(%p, %lx)\n", pSample, dwUser);
1050
1051     if (!pSample)
1052         return E_POINTER;
1053
1054     /* get start and stop positions in bytes */
1055     if (SUCCEEDED(hr))
1056         hr = IMediaSample_GetTime(pSample, &Start, &Stop);
1057
1058     if (SUCCEEDED(hr))
1059         hr = IMediaSample_GetPointer(pSample, &pBuffer);
1060
1061     EnterCriticalSection(&This->csList);
1062     if (This->bFlushing)
1063     {
1064         LeaveCriticalSection(&This->csList);
1065         return VFW_E_WRONG_STATE;
1066     }
1067
1068     if (SUCCEEDED(hr))
1069     {
1070         DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
1071         DATAREQUEST *pDataRq;
1072         int x;
1073
1074         for (x = 0; x < This->samples; ++x)
1075         {
1076             if (!This->sample_list[x].pSample)
1077                 break;
1078         }
1079         assert(x < This->samples);
1080         InterlockedIncrement(&This->queued_number);
1081
1082         pDataRq = This->sample_list + x;
1083
1084         pDataRq->ovl.u.s.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
1085         pDataRq->ovl.u.s.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
1086         pDataRq->dwUserData = dwUser;
1087
1088         /* we violate traditional COM rules here by maintaining
1089          * a reference to the sample, but not calling AddRef, but
1090          * that's what MSDN says to do */
1091         pDataRq->pSample = pSample;
1092
1093         /* this is definitely not how it is implemented on Win9x
1094          * as they do not support async reads on files, but it is
1095          * sooo much easier to use this than messing around with threads!
1096          */
1097         if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1098             hr = HRESULT_FROM_WIN32(GetLastError());
1099
1100         /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1101         if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1102             hr = S_OK;
1103     }
1104
1105     LeaveCriticalSection(&This->csList);
1106
1107     TRACE("-- %x\n", hr);
1108     return hr;
1109 }
1110
1111 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1112 {
1113     HRESULT hr = S_OK;
1114     FileAsyncReader *This = impl_from_IAsyncReader(iface);
1115     DWORD buffer = ~0;
1116
1117     TRACE("(%u, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1118
1119     *ppSample = NULL;
1120     *pdwUser = 0;
1121
1122     if (!This->bFlushing)
1123     {
1124         if (!This->queued_number)
1125         {
1126             ERR("Called without samples in queue and not flushing!!\n");
1127             return E_FAIL;
1128         }
1129
1130         /* wait for an object to read, or time out */
1131         buffer = WaitForMultipleObjectsEx(This->samples, This->handle_list, FALSE, dwTimeout, TRUE);
1132
1133         if (buffer >= MAXIMUM_WAIT_OBJECTS)
1134         {
1135             FIXME("Returned: %u (%08x)\n", buffer, GetLastError());
1136             hr = VFW_E_TIMEOUT;
1137             buffer = ~0;
1138         }
1139         else
1140             InterlockedDecrement(&This->queued_number);
1141     }
1142
1143     EnterCriticalSection(&This->csList);
1144     if (This->bFlushing && buffer == ~0)
1145     {
1146         for (buffer = 0; buffer < This->samples; ++buffer)
1147         {
1148             if (This->sample_list[buffer].pSample)
1149                 break;
1150         }
1151         if (buffer == This->samples)
1152         {
1153             assert(!This->queued_number);
1154             hr = E_FAIL;
1155         }
1156         else
1157         {
1158             InterlockedDecrement(&This->queued_number);
1159             hr = S_OK;
1160         }
1161     }
1162
1163     if (SUCCEEDED(hr))
1164     {
1165         REFERENCE_TIME rtStart, rtStop;
1166         REFERENCE_TIME rtSampleStart, rtSampleStop;
1167         DATAREQUEST *pDataRq = This->sample_list + buffer;
1168         DWORD dwBytes = 0;
1169
1170         /* get any errors */
1171         if (!This->bFlushing && !GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1172             hr = HRESULT_FROM_WIN32(GetLastError());
1173
1174         /* Return the sample no matter what so it can be destroyed */
1175         *ppSample = pDataRq->pSample;
1176         *pdwUser = pDataRq->dwUserData;
1177
1178         if (This->bFlushing)
1179             hr = VFW_E_WRONG_STATE;
1180
1181         if (FAILED(hr))
1182             dwBytes = 0;
1183
1184         /* Set the time on the sample */
1185         IMediaSample_SetActualDataLength(pDataRq->pSample, dwBytes);
1186
1187         rtStart = (DWORD64)pDataRq->ovl.u.s.Offset + ((DWORD64)pDataRq->ovl.u.s.OffsetHigh << 32);
1188         rtStart = MEDIATIME_FROM_BYTES(rtStart);
1189         rtStop = rtStart + MEDIATIME_FROM_BYTES(dwBytes);
1190
1191         IMediaSample_GetTime(pDataRq->pSample, &rtSampleStart, &rtSampleStop);
1192         assert(rtStart == rtSampleStart);
1193         assert(rtStop <= rtSampleStop);
1194
1195         IMediaSample_SetTime(pDataRq->pSample, &rtStart, &rtStop);
1196         assert(rtStart == rtSampleStart);
1197         if (hr == S_OK)
1198             assert(rtStop == rtSampleStop);
1199         else
1200             assert(rtStop == rtStart);
1201
1202         This->sample_list[buffer].pSample = NULL;
1203     }
1204     LeaveCriticalSection(&This->csList);
1205
1206     TRACE("-- %x\n", hr);
1207     return hr;
1208 }
1209
1210 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1211
1212 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1213 {
1214     BYTE * pBuffer;
1215     REFERENCE_TIME tStart;
1216     REFERENCE_TIME tStop;
1217     HRESULT hr;
1218
1219     TRACE("(%p)\n", pSample);
1220
1221     hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1222
1223     if (SUCCEEDED(hr))
1224         hr = IMediaSample_GetPointer(pSample, &pBuffer);
1225
1226     if (SUCCEEDED(hr))
1227         hr = FileAsyncReader_SyncRead(iface, 
1228             BYTES_FROM_MEDIATIME(tStart),
1229             (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1230             pBuffer);
1231
1232     TRACE("-- %x\n", hr);
1233     return hr;
1234 }
1235
1236 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1237 {
1238     OVERLAPPED ovl;
1239     HRESULT hr = S_OK;
1240     FileAsyncReader *This = impl_from_IAsyncReader(iface);
1241
1242     TRACE("(%x%08x, %d, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1243
1244     ZeroMemory(&ovl, sizeof(ovl));
1245
1246     ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1247     /* NOTE: llPosition is the actual byte position to start reading from */
1248     ovl.u.s.Offset = (DWORD) llPosition;
1249     ovl.u.s.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1250
1251     if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1252         hr = HRESULT_FROM_WIN32(GetLastError());
1253
1254     if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1255         hr = S_OK;
1256
1257     if (SUCCEEDED(hr))
1258     {
1259         DWORD dwBytesRead;
1260
1261         if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1262             hr = HRESULT_FROM_WIN32(GetLastError());
1263     }
1264
1265     CloseHandle(ovl.hEvent);
1266
1267     TRACE("-- %x\n", hr);
1268     return hr;
1269 }
1270
1271 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1272 {
1273     DWORD dwSizeLow;
1274     DWORD dwSizeHigh;
1275     FileAsyncReader *This = impl_from_IAsyncReader(iface);
1276
1277     TRACE("(%p, %p)\n", pTotal, pAvailable);
1278
1279     if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1280         (GetLastError() != NO_ERROR))
1281         return HRESULT_FROM_WIN32(GetLastError());
1282
1283     *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1284
1285     *pAvailable = *pTotal;
1286
1287     return S_OK;
1288 }
1289
1290 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1291 {
1292     FileAsyncReader *This = impl_from_IAsyncReader(iface);
1293
1294     TRACE("()\n");
1295
1296     EnterCriticalSection(&This->csList);
1297     This->bFlushing = TRUE;
1298     CancelIo(This->hFile);
1299     LeaveCriticalSection(&This->csList);
1300
1301     return S_OK;
1302 }
1303
1304 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1305 {
1306     FileAsyncReader *This = impl_from_IAsyncReader(iface);
1307
1308     TRACE("()\n");
1309
1310     EnterCriticalSection(&This->csList);
1311     This->bFlushing = FALSE;
1312     LeaveCriticalSection(&This->csList);
1313
1314     return S_OK;
1315 }
1316
1317 static const IAsyncReaderVtbl FileAsyncReader_Vtbl = 
1318 {
1319     FileAsyncReader_QueryInterface,
1320     FileAsyncReader_AddRef,
1321     FileAsyncReader_Release,
1322     FileAsyncReader_RequestAllocator,
1323     FileAsyncReader_Request,
1324     FileAsyncReader_WaitForNext,
1325     FileAsyncReader_SyncReadAligned,
1326     FileAsyncReader_SyncRead,
1327     FileAsyncReader_Length,
1328     FileAsyncReader_BeginFlush,
1329     FileAsyncReader_EndFlush,
1330 };