e63a5f42cdcc52be9ebcd6db43528e64acc4c884
[reactos.git] / reactos / dll / directx / wine / 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 #include "quartz_private.h"
22
23 static const WCHAR wszOutputPinName[] = { 'O','u','t','p','u','t',0 };
24
25 typedef struct AsyncReader
26 {
27 BaseFilter filter;
28 IFileSourceFilter IFileSourceFilter_iface;
29 IAMFilterMiscFlags IAMFilterMiscFlags_iface;
30
31 IPin * pOutputPin;
32 LPOLESTR pszFileName;
33 AM_MEDIA_TYPE * pmt;
34 } AsyncReader;
35
36 static inline AsyncReader *impl_from_BaseFilter(BaseFilter *iface)
37 {
38 return CONTAINING_RECORD(iface, AsyncReader, filter);
39 }
40
41 static inline AsyncReader *impl_from_IBaseFilter(IBaseFilter *iface)
42 {
43 return CONTAINING_RECORD(iface, AsyncReader, filter.IBaseFilter_iface);
44 }
45
46 static inline AsyncReader *impl_from_IFileSourceFilter(IFileSourceFilter *iface)
47 {
48 return CONTAINING_RECORD(iface, AsyncReader, IFileSourceFilter_iface);
49 }
50
51 static inline AsyncReader *impl_from_IAMFilterMiscFlags(IAMFilterMiscFlags *iface)
52 {
53 return CONTAINING_RECORD(iface, AsyncReader, IAMFilterMiscFlags_iface);
54 }
55
56 static const IBaseFilterVtbl AsyncReader_Vtbl;
57 static const IFileSourceFilterVtbl FileSource_Vtbl;
58 static const IAsyncReaderVtbl FileAsyncReader_Vtbl;
59 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl;
60
61 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin);
62
63 static const WCHAR mediatype_name[] = {
64 'M', 'e', 'd', 'i', 'a', ' ', 'T', 'y', 'p', 'e', 0 };
65 static const WCHAR subtype_name[] = {
66 'S', 'u', 'b', 't', 'y', 'p', 'e', 0 };
67 static const WCHAR source_filter_name[] = {
68 'S','o','u','r','c','e',' ','F','i','l','t','e','r',0};
69
70 static HRESULT process_extensions(HKEY hkeyExtensions, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType, GUID * sourceFilter)
71 {
72 WCHAR *extension;
73 LONG l;
74 HKEY hsub;
75 WCHAR keying[39];
76 DWORD size;
77
78 if (!pszFileName)
79 return E_POINTER;
80
81 /* Get the part of the name that matters */
82 extension = PathFindExtensionW(pszFileName);
83 if (*extension != '.')
84 return E_FAIL;
85
86 l = RegOpenKeyExW(hkeyExtensions, extension, 0, KEY_READ, &hsub);
87 if (l)
88 return E_FAIL;
89
90 if (majorType)
91 {
92 size = sizeof(keying);
93 l = RegQueryValueExW(hsub, mediatype_name, NULL, NULL, (LPBYTE)keying, &size);
94 if (!l)
95 CLSIDFromString(keying, majorType);
96 }
97
98 if (minorType)
99 {
100 size = sizeof(keying);
101 if (!l)
102 l = RegQueryValueExW(hsub, subtype_name, NULL, NULL, (LPBYTE)keying, &size);
103 if (!l)
104 CLSIDFromString(keying, minorType);
105 }
106
107 if (sourceFilter)
108 {
109 size = sizeof(keying);
110 if (!l)
111 l = RegQueryValueExW(hsub, source_filter_name, NULL, NULL, (LPBYTE)keying, &size);
112 if (!l)
113 CLSIDFromString(keying, sourceFilter);
114 }
115
116 RegCloseKey(hsub);
117
118 if (!l)
119 return S_OK;
120 return E_FAIL;
121 }
122
123 static unsigned char byte_from_hex_char(WCHAR wHex)
124 {
125 switch (tolowerW(wHex))
126 {
127 case '0':
128 case '1':
129 case '2':
130 case '3':
131 case '4':
132 case '5':
133 case '6':
134 case '7':
135 case '8':
136 case '9':
137 return (wHex - '0') & 0xf;
138 case 'a':
139 case 'b':
140 case 'c':
141 case 'd':
142 case 'e':
143 case 'f':
144 return (wHex - 'a' + 10) & 0xf;
145 default:
146 return 0;
147 }
148 }
149
150 static HRESULT process_pattern_string(LPCWSTR wszPatternString, IAsyncReader * pReader)
151 {
152 ULONG ulOffset;
153 ULONG ulBytes;
154 BYTE * pbMask;
155 BYTE * pbValue;
156 BYTE * pbFile;
157 HRESULT hr = S_OK;
158 ULONG strpos;
159
160 TRACE("\t\tPattern string: %s\n", debugstr_w(wszPatternString));
161
162 /* format: "offset, bytestocompare, mask, value" */
163
164 ulOffset = strtolW(wszPatternString, NULL, 10);
165
166 if (!(wszPatternString = strchrW(wszPatternString, ',')))
167 return E_INVALIDARG;
168
169 wszPatternString++; /* skip ',' */
170
171 ulBytes = strtolW(wszPatternString, NULL, 10);
172
173 pbMask = HeapAlloc(GetProcessHeap(), 0, ulBytes);
174 pbValue = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, ulBytes);
175 pbFile = HeapAlloc(GetProcessHeap(), 0, ulBytes);
176
177 /* default mask is match everything */
178 memset(pbMask, 0xFF, ulBytes);
179
180 if (!(wszPatternString = strchrW(wszPatternString, ',')))
181 hr = E_INVALIDARG;
182
183 if (hr == S_OK)
184 {
185 wszPatternString++; /* skip ',' */
186 while (!isxdigitW(*wszPatternString) && (*wszPatternString != ',')) wszPatternString++;
187
188 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
189 {
190 if ((strpos % 2) == 1) /* odd numbered position */
191 pbMask[strpos / 2] |= byte_from_hex_char(*wszPatternString);
192 else
193 pbMask[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
194 }
195
196 if (!(wszPatternString = strchrW(wszPatternString, ',')))
197 hr = E_INVALIDARG;
198 else
199 wszPatternString++; /* skip ',' */
200 }
201
202 if (hr == S_OK)
203 {
204 for ( ; !isxdigitW(*wszPatternString) && (*wszPatternString != ','); wszPatternString++)
205 ;
206
207 for (strpos = 0; isxdigitW(*wszPatternString) && (strpos/2 < ulBytes); wszPatternString++, strpos++)
208 {
209 if ((strpos % 2) == 1) /* odd numbered position */
210 pbValue[strpos / 2] |= byte_from_hex_char(*wszPatternString);
211 else
212 pbValue[strpos / 2] = byte_from_hex_char(*wszPatternString) << 4;
213 }
214 }
215
216 if (hr == S_OK)
217 hr = IAsyncReader_SyncRead(pReader, ulOffset, ulBytes, pbFile);
218
219 if (hr == S_OK)
220 {
221 ULONG i;
222 for (i = 0; i < ulBytes; i++)
223 if ((pbFile[i] & pbMask[i]) != pbValue[i])
224 {
225 hr = S_FALSE;
226 break;
227 }
228 }
229
230 HeapFree(GetProcessHeap(), 0, pbMask);
231 HeapFree(GetProcessHeap(), 0, pbValue);
232 HeapFree(GetProcessHeap(), 0, pbFile);
233
234 /* if we encountered no errors with this string, and there is a following tuple, then we
235 * have to match that as well to succeed */
236 if ((hr == S_OK) && (wszPatternString = strchrW(wszPatternString, ',')))
237 return process_pattern_string(wszPatternString + 1, pReader);
238 else
239 return hr;
240 }
241
242 HRESULT GetClassMediaFile(IAsyncReader * pReader, LPCOLESTR pszFileName, GUID * majorType, GUID * minorType, GUID * sourceFilter)
243 {
244 HKEY hkeyMediaType = NULL;
245 LONG lRet;
246 HRESULT hr = S_OK;
247 BOOL bFound = FALSE;
248 static const WCHAR wszMediaType[] = {'M','e','d','i','a',' ','T','y','p','e',0};
249
250 TRACE("(%p, %s, %p, %p)\n", pReader, debugstr_w(pszFileName), majorType, minorType);
251
252 if(majorType)
253 *majorType = GUID_NULL;
254 if(minorType)
255 *minorType = GUID_NULL;
256 if(sourceFilter)
257 *sourceFilter = GUID_NULL;
258
259 lRet = RegOpenKeyExW(HKEY_CLASSES_ROOT, wszMediaType, 0, KEY_READ, &hkeyMediaType);
260 hr = HRESULT_FROM_WIN32(lRet);
261
262 if (SUCCEEDED(hr))
263 {
264 DWORD indexMajor;
265
266 for (indexMajor = 0; !bFound; indexMajor++)
267 {
268 HKEY hkeyMajor;
269 WCHAR wszMajorKeyName[CHARS_IN_GUID];
270 DWORD dwKeyNameLength = sizeof(wszMajorKeyName) / sizeof(wszMajorKeyName[0]);
271 static const WCHAR wszExtensions[] = {'E','x','t','e','n','s','i','o','n','s',0};
272
273 if (RegEnumKeyExW(hkeyMediaType, indexMajor, wszMajorKeyName, &dwKeyNameLength, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
274 break;
275 if (RegOpenKeyExW(hkeyMediaType, wszMajorKeyName, 0, KEY_READ, &hkeyMajor) != ERROR_SUCCESS)
276 break;
277 TRACE("%s\n", debugstr_w(wszMajorKeyName));
278 if (!strcmpW(wszExtensions, wszMajorKeyName))
279 {
280 if (process_extensions(hkeyMajor, pszFileName, majorType, minorType, sourceFilter) == S_OK)
281 bFound = TRUE;
282 }
283 /* We need a reader interface to check bytes */
284 else if (pReader)
285 {
286 DWORD indexMinor;
287
288 for (indexMinor = 0; !bFound; indexMinor++)
289 {
290 HKEY hkeyMinor;
291 WCHAR wszMinorKeyName[CHARS_IN_GUID];
292 DWORD dwMinorKeyNameLen = sizeof(wszMinorKeyName) / sizeof(wszMinorKeyName[0]);
293 WCHAR wszSourceFilterKeyName[CHARS_IN_GUID];
294 DWORD dwSourceFilterKeyNameLen = sizeof(wszSourceFilterKeyName);
295 DWORD maxValueLen;
296 DWORD indexValue;
297
298 if (RegEnumKeyExW(hkeyMajor, indexMinor, wszMinorKeyName, &dwMinorKeyNameLen, NULL, NULL, NULL, NULL) != ERROR_SUCCESS)
299 break;
300
301 if (RegOpenKeyExW(hkeyMajor, wszMinorKeyName, 0, KEY_READ, &hkeyMinor) != ERROR_SUCCESS)
302 break;
303
304 TRACE("\t%s\n", debugstr_w(wszMinorKeyName));
305
306 if (RegQueryInfoKeyW(hkeyMinor, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &maxValueLen, NULL, NULL) != ERROR_SUCCESS)
307 break;
308
309 for (indexValue = 0; !bFound; indexValue++)
310 {
311 DWORD dwType;
312 WCHAR wszValueName[14]; /* longest name we should encounter will be "Source Filter" */
313 LPWSTR wszPatternString = HeapAlloc(GetProcessHeap(), 0, maxValueLen);
314 DWORD dwValueNameLen = sizeof(wszValueName) / sizeof(wszValueName[0]); /* remember this is in chars */
315 DWORD dwDataLen = maxValueLen; /* remember this is in bytes */
316
317 if (RegEnumValueW(hkeyMinor, indexValue, wszValueName, &dwValueNameLen, NULL, &dwType, (LPBYTE)wszPatternString, &dwDataLen) != ERROR_SUCCESS)
318 {
319 HeapFree(GetProcessHeap(), 0, wszPatternString);
320 break;
321 }
322
323 if (strcmpW(wszValueName, source_filter_name)==0) {
324 HeapFree(GetProcessHeap(), 0, wszPatternString);
325 continue;
326 }
327
328 /* if it is not the source filter value */
329 if (process_pattern_string(wszPatternString, pReader) == S_OK)
330 {
331 HeapFree(GetProcessHeap(), 0, wszPatternString);
332 if (majorType && FAILED(CLSIDFromString(wszMajorKeyName, majorType)))
333 break;
334 if (minorType && FAILED(CLSIDFromString(wszMinorKeyName, minorType)))
335 break;
336 if (sourceFilter)
337 {
338 /* Look up the source filter key */
339 if (RegQueryValueExW(hkeyMinor, source_filter_name, NULL, NULL, (LPBYTE)wszSourceFilterKeyName, &dwSourceFilterKeyNameLen))
340 break;
341 if (FAILED(CLSIDFromString(wszSourceFilterKeyName, sourceFilter)))
342 break;
343 }
344 bFound = TRUE;
345 } else
346 HeapFree(GetProcessHeap(), 0, wszPatternString);
347 }
348 CloseHandle(hkeyMinor);
349 }
350 }
351 CloseHandle(hkeyMajor);
352 }
353 }
354 CloseHandle(hkeyMediaType);
355
356 if (SUCCEEDED(hr) && !bFound)
357 {
358 ERR("Media class not found\n");
359 hr = E_FAIL;
360 }
361 else if (bFound)
362 {
363 TRACE("Found file's class:\n");
364 if(majorType)
365 TRACE("\tmajor = %s\n", qzdebugstr_guid(majorType));
366 if(minorType)
367 TRACE("\tsubtype = %s\n", qzdebugstr_guid(minorType));
368 if(sourceFilter)
369 TRACE("\tsource filter = %s\n", qzdebugstr_guid(sourceFilter));
370 }
371
372 return hr;
373 }
374
375 static IPin* WINAPI AsyncReader_GetPin(BaseFilter *iface, int pos)
376 {
377 AsyncReader *This = impl_from_BaseFilter(iface);
378
379 if (pos >= 1 || !This->pOutputPin)
380 return NULL;
381
382 IPin_AddRef(This->pOutputPin);
383 return This->pOutputPin;
384 }
385
386 static LONG WINAPI AsyncReader_GetPinCount(BaseFilter *iface)
387 {
388 AsyncReader *This = impl_from_BaseFilter(iface);
389
390 if (!This->pOutputPin)
391 return 0;
392 else
393 return 1;
394 }
395
396 static const BaseFilterFuncTable BaseFuncTable = {
397 AsyncReader_GetPin,
398 AsyncReader_GetPinCount
399 };
400
401 HRESULT AsyncReader_create(IUnknown * pUnkOuter, LPVOID * ppv)
402 {
403 AsyncReader *pAsyncRead;
404
405 if( pUnkOuter )
406 return CLASS_E_NOAGGREGATION;
407
408 pAsyncRead = CoTaskMemAlloc(sizeof(AsyncReader));
409
410 if (!pAsyncRead)
411 return E_OUTOFMEMORY;
412
413 BaseFilter_Init(&pAsyncRead->filter, &AsyncReader_Vtbl, &CLSID_AsyncReader, (DWORD_PTR)(__FILE__ ": AsyncReader.csFilter"), &BaseFuncTable);
414
415 pAsyncRead->IFileSourceFilter_iface.lpVtbl = &FileSource_Vtbl;
416 pAsyncRead->IAMFilterMiscFlags_iface.lpVtbl = &IAMFilterMiscFlags_Vtbl;
417 pAsyncRead->pOutputPin = NULL;
418
419 pAsyncRead->pszFileName = NULL;
420 pAsyncRead->pmt = NULL;
421
422 *ppv = pAsyncRead;
423
424 TRACE("-- created at %p\n", pAsyncRead);
425
426 return S_OK;
427 }
428
429 /** IUnknown methods **/
430
431 static HRESULT WINAPI AsyncReader_QueryInterface(IBaseFilter * iface, REFIID riid, LPVOID * ppv)
432 {
433 AsyncReader *This = impl_from_IBaseFilter(iface);
434
435 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
436
437 *ppv = NULL;
438
439 if (IsEqualIID(riid, &IID_IUnknown))
440 *ppv = This;
441 else if (IsEqualIID(riid, &IID_IPersist))
442 *ppv = This;
443 else if (IsEqualIID(riid, &IID_IMediaFilter))
444 *ppv = This;
445 else if (IsEqualIID(riid, &IID_IBaseFilter))
446 *ppv = This;
447 else if (IsEqualIID(riid, &IID_IFileSourceFilter))
448 *ppv = &This->IFileSourceFilter_iface;
449 else if (IsEqualIID(riid, &IID_IAMFilterMiscFlags))
450 *ppv = &This->IAMFilterMiscFlags_iface;
451
452 if (*ppv)
453 {
454 IUnknown_AddRef((IUnknown *)(*ppv));
455 return S_OK;
456 }
457
458 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking) &&
459 !IsEqualIID(riid, &IID_IVideoWindow) && !IsEqualIID(riid, &IID_IBasicAudio))
460 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
461
462 return E_NOINTERFACE;
463 }
464
465 static ULONG WINAPI AsyncReader_Release(IBaseFilter * iface)
466 {
467 AsyncReader *This = impl_from_IBaseFilter(iface);
468 ULONG refCount = BaseFilterImpl_Release(iface);
469
470 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
471
472 if (!refCount)
473 {
474 if (This->pOutputPin)
475 {
476 IPin *pConnectedTo;
477 if(SUCCEEDED(IPin_ConnectedTo(This->pOutputPin, &pConnectedTo)))
478 {
479 IPin_Disconnect(pConnectedTo);
480 IPin_Release(pConnectedTo);
481 }
482 IPin_Disconnect(This->pOutputPin);
483 IPin_Release(This->pOutputPin);
484 }
485 CoTaskMemFree(This->pszFileName);
486 if (This->pmt)
487 FreeMediaType(This->pmt);
488 CoTaskMemFree(This);
489 return 0;
490 }
491 else
492 return refCount;
493 }
494
495 /** IMediaFilter methods **/
496
497 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
498 {
499 AsyncReader *This = impl_from_IBaseFilter(iface);
500
501 TRACE("()\n");
502
503 This->filter.state = State_Stopped;
504
505 return S_OK;
506 }
507
508 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
509 {
510 AsyncReader *This = impl_from_IBaseFilter(iface);
511
512 TRACE("()\n");
513
514 This->filter.state = State_Paused;
515
516 return S_OK;
517 }
518
519 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
520 {
521 AsyncReader *This = impl_from_IBaseFilter(iface);
522
523 TRACE("(%x%08x)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
524
525 This->filter.state = State_Running;
526
527 return S_OK;
528 }
529
530 /** IBaseFilter methods **/
531
532 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
533 {
534 AsyncReader *This = impl_from_IBaseFilter(iface);
535 TRACE("(%s, %p)\n", debugstr_w(Id), ppPin);
536
537 if (!Id || !ppPin)
538 return E_POINTER;
539
540 if (strcmpW(Id, wszOutputPinName))
541 {
542 *ppPin = NULL;
543 return VFW_E_NOT_FOUND;
544 }
545
546 *ppPin = This->pOutputPin;
547 IPin_AddRef(*ppPin);
548 return S_OK;
549 }
550
551 static const IBaseFilterVtbl AsyncReader_Vtbl =
552 {
553 AsyncReader_QueryInterface,
554 BaseFilterImpl_AddRef,
555 AsyncReader_Release,
556 BaseFilterImpl_GetClassID,
557 AsyncReader_Stop,
558 AsyncReader_Pause,
559 AsyncReader_Run,
560 BaseFilterImpl_GetState,
561 BaseFilterImpl_SetSyncSource,
562 BaseFilterImpl_GetSyncSource,
563 BaseFilterImpl_EnumPins,
564 AsyncReader_FindPin,
565 BaseFilterImpl_QueryFilterInfo,
566 BaseFilterImpl_JoinFilterGraph,
567 BaseFilterImpl_QueryVendorInfo
568 };
569
570 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
571 {
572 AsyncReader *This = impl_from_IFileSourceFilter(iface);
573
574 return IBaseFilter_QueryInterface(&This->filter.IBaseFilter_iface, riid, ppv);
575 }
576
577 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
578 {
579 AsyncReader *This = impl_from_IFileSourceFilter(iface);
580
581 return IBaseFilter_AddRef(&This->filter.IBaseFilter_iface);
582 }
583
584 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
585 {
586 AsyncReader *This = impl_from_IFileSourceFilter(iface);
587
588 return IBaseFilter_Release(&This->filter.IBaseFilter_iface);
589 }
590
591 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
592 {
593 HRESULT hr;
594 HANDLE hFile;
595 IAsyncReader * pReader = NULL;
596 AsyncReader *This = impl_from_IFileSourceFilter(iface);
597
598 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
599
600 /* open file */
601 /* FIXME: check the sharing values that native uses */
602 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
603
604 if (hFile == INVALID_HANDLE_VALUE)
605 {
606 return HRESULT_FROM_WIN32(GetLastError());
607 }
608
609 /* create pin */
610 hr = FileAsyncReader_Construct(hFile, &This->filter.IBaseFilter_iface, &This->filter.csFilter, &This->pOutputPin);
611 BaseFilterImpl_IncrementPinVersion(&This->filter);
612
613 if (SUCCEEDED(hr))
614 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
615
616 /* store file name & media type */
617 if (SUCCEEDED(hr))
618 {
619 CoTaskMemFree(This->pszFileName);
620 if (This->pmt)
621 FreeMediaType(This->pmt);
622
623 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
624 strcpyW(This->pszFileName, pszFileName);
625
626 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
627 if (!pmt)
628 {
629 This->pmt->bFixedSizeSamples = TRUE;
630 This->pmt->bTemporalCompression = FALSE;
631 This->pmt->cbFormat = 0;
632 This->pmt->pbFormat = NULL;
633 This->pmt->pUnk = NULL;
634 This->pmt->lSampleSize = 0;
635 This->pmt->formattype = FORMAT_None;
636 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype, NULL);
637 if (FAILED(hr))
638 {
639 CoTaskMemFree(This->pmt);
640 This->pmt = NULL;
641 }
642 }
643 else
644 CopyMediaType(This->pmt, pmt);
645 }
646
647 if (pReader)
648 IAsyncReader_Release(pReader);
649
650 if (FAILED(hr))
651 {
652 if (This->pOutputPin)
653 {
654 IPin_Release(This->pOutputPin);
655 This->pOutputPin = NULL;
656 }
657
658 CoTaskMemFree(This->pszFileName);
659 if (This->pmt)
660 FreeMediaType(This->pmt);
661 This->pszFileName = NULL;
662 This->pmt = NULL;
663
664 CloseHandle(hFile);
665 }
666
667 /* FIXME: check return codes */
668 return hr;
669 }
670
671 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
672 {
673 AsyncReader *This = impl_from_IFileSourceFilter(iface);
674
675 TRACE("(%p, %p)\n", ppszFileName, pmt);
676
677 if (!ppszFileName)
678 return E_POINTER;
679
680 /* copy file name & media type if available, otherwise clear the outputs */
681 if (This->pszFileName)
682 {
683 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
684 strcpyW(*ppszFileName, This->pszFileName);
685 }
686 else
687 *ppszFileName = NULL;
688
689 if (pmt)
690 {
691 if (This->pmt)
692 CopyMediaType(pmt, This->pmt);
693 else
694 ZeroMemory(pmt, sizeof(*pmt));
695 }
696
697 return S_OK;
698 }
699
700 static const IFileSourceFilterVtbl FileSource_Vtbl =
701 {
702 FileSource_QueryInterface,
703 FileSource_AddRef,
704 FileSource_Release,
705 FileSource_Load,
706 FileSource_GetCurFile
707 };
708
709
710 /* the dwUserData passed back to user */
711 typedef struct DATAREQUEST
712 {
713 IMediaSample * pSample; /* sample passed to us by user */
714 DWORD_PTR dwUserData; /* user data passed to us */
715 OVERLAPPED ovl; /* our overlapped structure */
716 } DATAREQUEST;
717
718 typedef struct FileAsyncReader
719 {
720 BaseOutputPin pin;
721 IAsyncReader IAsyncReader_iface;
722
723 ALLOCATOR_PROPERTIES allocProps;
724 HANDLE hFile;
725 BOOL bFlushing;
726 /* Why would you need more? Every sample has its own handle */
727 LONG queued_number;
728 LONG samples;
729 LONG oldest_sample;
730 CRITICAL_SECTION csList; /* critical section to prevent concurrency issues */
731 DATAREQUEST *sample_list;
732
733 /* Have a handle for every sample, and then one more as flushing handle */
734 HANDLE *handle_list;
735 } FileAsyncReader;
736
737 static inline FileAsyncReader *impl_from_IPin(IPin *iface)
738 {
739 return CONTAINING_RECORD(iface, FileAsyncReader, pin.pin.IPin_iface);
740 }
741
742 static inline FileAsyncReader *impl_from_BasePin(BasePin *iface)
743 {
744 return CONTAINING_RECORD(iface, FileAsyncReader, pin.pin);
745 }
746
747 static inline FileAsyncReader *impl_from_BaseOutputPin(BaseOutputPin *iface)
748 {
749 return CONTAINING_RECORD(iface, FileAsyncReader, pin);
750 }
751
752 static inline BaseOutputPin *impl_BaseOututPin_from_BasePin(BasePin *iface)
753 {
754 return CONTAINING_RECORD(iface, BaseOutputPin, pin);
755 }
756
757 static inline FileAsyncReader *impl_from_IAsyncReader(IAsyncReader *iface)
758 {
759 return CONTAINING_RECORD(iface, FileAsyncReader, IAsyncReader_iface);
760 }
761
762 static HRESULT WINAPI FileAsyncReaderPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *pmt)
763 {
764 FileAsyncReader *This = impl_from_IPin(iface);
765 AM_MEDIA_TYPE *pmt_filter = impl_from_IBaseFilter(This->pin.pin.pinInfo.pFilter)->pmt;
766
767 FIXME("(%p, %p)\n", iface, pmt);
768
769 if (IsEqualGUID(&pmt->majortype, &pmt_filter->majortype) &&
770 IsEqualGUID(&pmt->subtype, &pmt_filter->subtype) &&
771 IsEqualGUID(&pmt->formattype, &FORMAT_None))
772 return S_OK;
773
774 return S_FALSE;
775 }
776
777 static HRESULT WINAPI FileAsyncReaderPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
778 {
779 FileAsyncReader *This = impl_from_BasePin(iface);
780 if (iPosition < 0)
781 return E_INVALIDARG;
782 if (iPosition > 0)
783 return VFW_S_NO_MORE_ITEMS;
784 CopyMediaType(pmt, impl_from_IBaseFilter(This->pin.pin.pinInfo.pFilter)->pmt);
785 return S_OK;
786 }
787
788 /* overridden pin functions */
789
790 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
791 {
792 FileAsyncReader *This = impl_from_IPin(iface);
793 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
794
795 *ppv = NULL;
796
797 if (IsEqualIID(riid, &IID_IUnknown))
798 *ppv = This;
799 else if (IsEqualIID(riid, &IID_IPin))
800 *ppv = This;
801 else if (IsEqualIID(riid, &IID_IAsyncReader))
802 *ppv = &This->IAsyncReader_iface;
803
804 if (*ppv)
805 {
806 IUnknown_AddRef((IUnknown *)(*ppv));
807 return S_OK;
808 }
809
810 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking))
811 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
812
813 return E_NOINTERFACE;
814 }
815
816 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
817 {
818 FileAsyncReader *This = impl_from_IPin(iface);
819 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
820 int x;
821
822 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
823
824 if (!refCount)
825 {
826 CoTaskMemFree(This->sample_list);
827 if (This->handle_list)
828 {
829 for (x = 0; x <= This->samples; ++x)
830 CloseHandle(This->handle_list[x]);
831 CoTaskMemFree(This->handle_list);
832 }
833 CloseHandle(This->hFile);
834 This->csList.DebugInfo->Spare[0] = 0;
835 DeleteCriticalSection(&This->csList);
836 CoTaskMemFree(This);
837 return 0;
838 }
839 return refCount;
840 }
841
842 static const IPinVtbl FileAsyncReaderPin_Vtbl =
843 {
844 FileAsyncReaderPin_QueryInterface,
845 BasePinImpl_AddRef,
846 FileAsyncReaderPin_Release,
847 BaseOutputPinImpl_Connect,
848 BaseOutputPinImpl_ReceiveConnection,
849 BasePinImpl_Disconnect,
850 BasePinImpl_ConnectedTo,
851 BasePinImpl_ConnectionMediaType,
852 BasePinImpl_QueryPinInfo,
853 BasePinImpl_QueryDirection,
854 BasePinImpl_QueryId,
855 FileAsyncReaderPin_QueryAccept,
856 BasePinImpl_EnumMediaTypes,
857 BasePinImpl_QueryInternalConnections,
858 BaseOutputPinImpl_EndOfStream,
859 BaseOutputPinImpl_BeginFlush,
860 BaseOutputPinImpl_EndFlush,
861 BasePinImpl_NewSegment
862 };
863
864 /* Function called as a helper to IPin_Connect */
865 /* specific AM_MEDIA_TYPE - it cannot be NULL */
866 /* this differs from standard OutputPin_AttemptConnection only in that it
867 * doesn't need the IMemInputPin interface on the receiving pin */
868 static HRESULT WINAPI FileAsyncReaderPin_AttemptConnection(BasePin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
869 {
870 BaseOutputPin *This = impl_BaseOututPin_from_BasePin(iface);
871 HRESULT hr;
872
873 TRACE("(%p, %p)\n", pReceivePin, pmt);
874 dump_AM_MEDIA_TYPE(pmt);
875
876 /* FIXME: call queryacceptproc */
877
878 This->pin.pConnectedTo = pReceivePin;
879 IPin_AddRef(pReceivePin);
880 CopyMediaType(&This->pin.mtCurrent, pmt);
881
882 hr = IPin_ReceiveConnection(pReceivePin, &iface->IPin_iface, pmt);
883
884 if (FAILED(hr))
885 {
886 IPin_Release(This->pin.pConnectedTo);
887 This->pin.pConnectedTo = NULL;
888 FreeMediaType(&This->pin.mtCurrent);
889 }
890
891 TRACE(" -- %x\n", hr);
892 return hr;
893 }
894
895 static HRESULT WINAPI FileAsyncReaderPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
896 {
897 FileAsyncReader *This = impl_from_BaseOutputPin(iface);
898 ALLOCATOR_PROPERTIES actual;
899
900 if (ppropInputRequest->cbAlign && ppropInputRequest->cbAlign != This->allocProps.cbAlign)
901 FIXME("Requested Buffer cbAlign mismatch %i,%i\n",This->allocProps.cbAlign, ppropInputRequest->cbAlign);
902 if (ppropInputRequest->cbPrefix)
903 FIXME("Requested Buffer cbPrefix mismatch %i,%i\n",This->allocProps.cbPrefix, ppropInputRequest->cbPrefix);
904 if (ppropInputRequest->cbBuffer)
905 FIXME("Requested Buffer cbBuffer mismatch %i,%i\n",This->allocProps.cbBuffer, ppropInputRequest->cbBuffer);
906 if (ppropInputRequest->cBuffers)
907 FIXME("Requested Buffer cBuffers mismatch %i,%i\n",This->allocProps.cBuffers, ppropInputRequest->cBuffers);
908
909 return IMemAllocator_SetProperties(pAlloc, &This->allocProps, &actual);
910 }
911
912 static const BaseOutputPinFuncTable output_BaseOutputFuncTable = {
913 {
914 NULL,
915 FileAsyncReaderPin_AttemptConnection,
916 BasePinImpl_GetMediaTypeVersion,
917 FileAsyncReaderPin_GetMediaType
918 },
919 FileAsyncReaderPin_DecideBufferSize,
920 BaseOutputPinImpl_DecideAllocator,
921 BaseOutputPinImpl_BreakConnect
922 };
923
924 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
925 {
926 PIN_INFO piOutput;
927 HRESULT hr;
928
929 *ppPin = NULL;
930 piOutput.dir = PINDIR_OUTPUT;
931 piOutput.pFilter = pBaseFilter;
932 strcpyW(piOutput.achName, wszOutputPinName);
933 hr = BaseOutputPin_Construct(&FileAsyncReaderPin_Vtbl, sizeof(FileAsyncReader), &piOutput, &output_BaseOutputFuncTable, pCritSec, ppPin);
934
935 if (SUCCEEDED(hr))
936 {
937 FileAsyncReader *pPinImpl = (FileAsyncReader *)*ppPin;
938 pPinImpl->IAsyncReader_iface.lpVtbl = &FileAsyncReader_Vtbl;
939 pPinImpl->hFile = hFile;
940 pPinImpl->bFlushing = FALSE;
941 pPinImpl->sample_list = NULL;
942 pPinImpl->handle_list = NULL;
943 pPinImpl->queued_number = 0;
944 InitializeCriticalSection(&pPinImpl->csList);
945 pPinImpl->csList.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FileAsyncReader.csList");
946 }
947 return hr;
948 }
949
950 /* IAsyncReader */
951
952 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
953 {
954 FileAsyncReader *This = impl_from_IAsyncReader(iface);
955
956 return IPin_QueryInterface(&This->pin.pin.IPin_iface, riid, ppv);
957 }
958
959 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
960 {
961 FileAsyncReader *This = impl_from_IAsyncReader(iface);
962
963 return IPin_AddRef(&This->pin.pin.IPin_iface);
964 }
965
966 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
967 {
968 FileAsyncReader *This = impl_from_IAsyncReader(iface);
969
970 return IPin_Release(&This->pin.pin.IPin_iface);
971 }
972
973 #define DEF_ALIGNMENT 1
974
975 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
976 {
977 FileAsyncReader *This = impl_from_IAsyncReader(iface);
978
979 HRESULT hr = S_OK;
980
981 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
982
983 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
984 pProps->cbAlign = DEF_ALIGNMENT;
985
986 if (pPreferred)
987 {
988 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
989 /* FIXME: check we are still aligned */
990 if (SUCCEEDED(hr))
991 {
992 IMemAllocator_AddRef(pPreferred);
993 *ppActual = pPreferred;
994 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
995 goto done;
996 }
997 }
998
999 pPreferred = NULL;
1000
1001 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
1002
1003 if (SUCCEEDED(hr))
1004 {
1005 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
1006 /* FIXME: check we are still aligned */
1007 if (SUCCEEDED(hr))
1008 {
1009 *ppActual = pPreferred;
1010 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
1011 }
1012 }
1013
1014 done:
1015 if (SUCCEEDED(hr))
1016 {
1017 CoTaskMemFree(This->sample_list);
1018 if (This->handle_list)
1019 {
1020 int x;
1021 for (x = 0; x <= This->samples; ++x)
1022 CloseHandle(This->handle_list[x]);
1023 CoTaskMemFree(This->handle_list);
1024 }
1025
1026 This->samples = pProps->cBuffers;
1027 This->oldest_sample = 0;
1028 TRACE("Samples: %u\n", This->samples);
1029 This->sample_list = CoTaskMemAlloc(sizeof(This->sample_list[0]) * pProps->cBuffers);
1030 This->handle_list = CoTaskMemAlloc(sizeof(HANDLE) * pProps->cBuffers * 2);
1031
1032 if (This->sample_list && This->handle_list)
1033 {
1034 int x;
1035 ZeroMemory(This->sample_list, sizeof(This->sample_list[0]) * pProps->cBuffers);
1036 for (x = 0; x < This->samples; ++x)
1037 {
1038 This->sample_list[x].ovl.hEvent = This->handle_list[x] = CreateEventW(NULL, 0, 0, NULL);
1039 if (x + 1 < This->samples)
1040 This->handle_list[This->samples + 1 + x] = This->handle_list[x];
1041 }
1042 This->handle_list[This->samples] = CreateEventW(NULL, 1, 0, NULL);
1043 This->allocProps = *pProps;
1044 }
1045 else
1046 {
1047 hr = E_OUTOFMEMORY;
1048 CoTaskMemFree(This->sample_list);
1049 CoTaskMemFree(This->handle_list);
1050 This->samples = 0;
1051 This->sample_list = NULL;
1052 This->handle_list = NULL;
1053 }
1054 }
1055
1056 if (FAILED(hr))
1057 {
1058 *ppActual = NULL;
1059 if (pPreferred)
1060 IMemAllocator_Release(pPreferred);
1061 }
1062
1063 TRACE("-- %x\n", hr);
1064 return hr;
1065 }
1066
1067 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
1068 * however, this would be quite complicated to do and may be a bit error prone */
1069 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
1070 {
1071 HRESULT hr = S_OK;
1072 REFERENCE_TIME Start;
1073 REFERENCE_TIME Stop;
1074 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1075 LPBYTE pBuffer = NULL;
1076
1077 TRACE("(%p, %lx)\n", pSample, dwUser);
1078
1079 if (!pSample)
1080 return E_POINTER;
1081
1082 /* get start and stop positions in bytes */
1083 if (SUCCEEDED(hr))
1084 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
1085
1086 if (SUCCEEDED(hr))
1087 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1088
1089 EnterCriticalSection(&This->csList);
1090 if (This->bFlushing)
1091 {
1092 LeaveCriticalSection(&This->csList);
1093 return VFW_E_WRONG_STATE;
1094 }
1095
1096 if (SUCCEEDED(hr))
1097 {
1098 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
1099 DATAREQUEST *pDataRq;
1100 int x;
1101
1102 /* Try to insert above the waiting sample if possible */
1103 for (x = This->oldest_sample; x < This->samples; ++x)
1104 {
1105 if (!This->sample_list[x].pSample)
1106 break;
1107 }
1108
1109 if (x >= This->samples)
1110 for (x = 0; x < This->oldest_sample; ++x)
1111 {
1112 if (!This->sample_list[x].pSample)
1113 break;
1114 }
1115
1116 /* There must be a sample we have found */
1117 assert(x < This->samples);
1118 ++This->queued_number;
1119
1120 pDataRq = This->sample_list + x;
1121
1122 pDataRq->ovl.u.s.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
1123 pDataRq->ovl.u.s.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
1124 pDataRq->dwUserData = dwUser;
1125
1126 /* we violate traditional COM rules here by maintaining
1127 * a reference to the sample, but not calling AddRef, but
1128 * that's what MSDN says to do */
1129 pDataRq->pSample = pSample;
1130
1131 /* this is definitely not how it is implemented on Win9x
1132 * as they do not support async reads on files, but it is
1133 * sooo much easier to use this than messing around with threads!
1134 */
1135 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1136 hr = HRESULT_FROM_WIN32(GetLastError());
1137
1138 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1139 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1140 hr = S_OK;
1141 }
1142
1143 LeaveCriticalSection(&This->csList);
1144
1145 TRACE("-- %x\n", hr);
1146 return hr;
1147 }
1148
1149 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1150 {
1151 HRESULT hr = S_OK;
1152 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1153 DWORD buffer = ~0;
1154
1155 TRACE("(%u, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1156
1157 *ppSample = NULL;
1158 *pdwUser = 0;
1159
1160 EnterCriticalSection(&This->csList);
1161 if (!This->bFlushing)
1162 {
1163 LONG oldest = This->oldest_sample;
1164
1165 if (!This->queued_number)
1166 {
1167 /* It could be that nothing is queued right now, but that can be fixed */
1168 WARN("Called without samples in queue and not flushing!!\n");
1169 }
1170 LeaveCriticalSection(&This->csList);
1171
1172 /* wait for an object to read, or time out */
1173 buffer = WaitForMultipleObjectsEx(This->samples+1, This->handle_list + oldest, FALSE, dwTimeout, TRUE);
1174
1175 EnterCriticalSection(&This->csList);
1176 if (buffer <= This->samples)
1177 {
1178 /* Re-scale the buffer back to normal */
1179 buffer += oldest;
1180
1181 /* Uh oh, we overshot the flusher handle, renormalize it back to 0..Samples-1 */
1182 if (buffer > This->samples)
1183 buffer -= This->samples + 1;
1184 assert(buffer <= This->samples);
1185 }
1186
1187 if (buffer >= This->samples)
1188 {
1189 if (buffer != This->samples)
1190 {
1191 FIXME("Returned: %u (%08x)\n", buffer, GetLastError());
1192 hr = VFW_E_TIMEOUT;
1193 }
1194 else
1195 hr = VFW_E_WRONG_STATE;
1196 buffer = ~0;
1197 }
1198 else
1199 --This->queued_number;
1200 }
1201
1202 if (This->bFlushing && buffer == ~0)
1203 {
1204 for (buffer = 0; buffer < This->samples; ++buffer)
1205 {
1206 if (This->sample_list[buffer].pSample)
1207 {
1208 ResetEvent(This->handle_list[buffer]);
1209 break;
1210 }
1211 }
1212 if (buffer == This->samples)
1213 {
1214 assert(!This->queued_number);
1215 hr = VFW_E_TIMEOUT;
1216 }
1217 else
1218 {
1219 --This->queued_number;
1220 hr = S_OK;
1221 }
1222 }
1223
1224 if (SUCCEEDED(hr))
1225 {
1226 REFERENCE_TIME rtStart, rtStop;
1227 REFERENCE_TIME rtSampleStart, rtSampleStop;
1228 DATAREQUEST *pDataRq = This->sample_list + buffer;
1229 DWORD dwBytes = 0;
1230
1231 /* get any errors */
1232 if (!This->bFlushing && !GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1233 hr = HRESULT_FROM_WIN32(GetLastError());
1234
1235 /* Return the sample no matter what so it can be destroyed */
1236 *ppSample = pDataRq->pSample;
1237 *pdwUser = pDataRq->dwUserData;
1238
1239 if (This->bFlushing)
1240 hr = VFW_E_WRONG_STATE;
1241
1242 if (FAILED(hr))
1243 dwBytes = 0;
1244
1245 /* Set the time on the sample */
1246 IMediaSample_SetActualDataLength(pDataRq->pSample, dwBytes);
1247
1248 rtStart = (DWORD64)pDataRq->ovl.u.s.Offset + ((DWORD64)pDataRq->ovl.u.s.OffsetHigh << 32);
1249 rtStart = MEDIATIME_FROM_BYTES(rtStart);
1250 rtStop = rtStart + MEDIATIME_FROM_BYTES(dwBytes);
1251
1252 IMediaSample_GetTime(pDataRq->pSample, &rtSampleStart, &rtSampleStop);
1253 assert(rtStart == rtSampleStart);
1254 assert(rtStop <= rtSampleStop);
1255
1256 IMediaSample_SetTime(pDataRq->pSample, &rtStart, &rtStop);
1257 assert(rtStart == rtSampleStart);
1258 if (hr == S_OK)
1259 assert(rtStop == rtSampleStop);
1260 else
1261 assert(rtStop == rtStart);
1262
1263 This->sample_list[buffer].pSample = NULL;
1264 assert(This->oldest_sample < This->samples);
1265
1266 if (buffer == This->oldest_sample)
1267 {
1268 LONG x;
1269 for (x = This->oldest_sample + 1; x < This->samples; ++x)
1270 if (This->sample_list[x].pSample)
1271 break;
1272 if (x >= This->samples)
1273 for (x = 0; x < This->oldest_sample; ++x)
1274 if (This->sample_list[x].pSample)
1275 break;
1276 if (This->oldest_sample == x)
1277 /* No samples found, reset to 0 */
1278 x = 0;
1279 This->oldest_sample = x;
1280 }
1281 }
1282 LeaveCriticalSection(&This->csList);
1283
1284 TRACE("-- %x\n", hr);
1285 return hr;
1286 }
1287
1288 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1289
1290 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1291 {
1292 BYTE * pBuffer;
1293 REFERENCE_TIME tStart;
1294 REFERENCE_TIME tStop;
1295 HRESULT hr;
1296
1297 TRACE("(%p)\n", pSample);
1298
1299 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1300
1301 if (SUCCEEDED(hr))
1302 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1303
1304 if (SUCCEEDED(hr))
1305 hr = FileAsyncReader_SyncRead(iface,
1306 BYTES_FROM_MEDIATIME(tStart),
1307 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1308 pBuffer);
1309
1310 TRACE("-- %x\n", hr);
1311 return hr;
1312 }
1313
1314 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1315 {
1316 OVERLAPPED ovl;
1317 HRESULT hr = S_OK;
1318 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1319
1320 TRACE("(%x%08x, %d, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1321
1322 ZeroMemory(&ovl, sizeof(ovl));
1323
1324 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1325 /* NOTE: llPosition is the actual byte position to start reading from */
1326 ovl.u.s.Offset = (DWORD) llPosition;
1327 ovl.u.s.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1328
1329 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1330 hr = HRESULT_FROM_WIN32(GetLastError());
1331
1332 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1333 hr = S_OK;
1334
1335 if (SUCCEEDED(hr))
1336 {
1337 DWORD dwBytesRead;
1338
1339 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1340 hr = HRESULT_FROM_WIN32(GetLastError());
1341 }
1342
1343 CloseHandle(ovl.hEvent);
1344
1345 TRACE("-- %x\n", hr);
1346 return hr;
1347 }
1348
1349 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1350 {
1351 DWORD dwSizeLow;
1352 DWORD dwSizeHigh;
1353 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1354
1355 TRACE("(%p, %p)\n", pTotal, pAvailable);
1356
1357 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1358 (GetLastError() != NO_ERROR))
1359 return HRESULT_FROM_WIN32(GetLastError());
1360
1361 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1362
1363 *pAvailable = *pTotal;
1364
1365 return S_OK;
1366 }
1367
1368 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1369 {
1370 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1371
1372 TRACE("()\n");
1373
1374 EnterCriticalSection(&This->csList);
1375 This->bFlushing = TRUE;
1376 CancelIo(This->hFile);
1377 SetEvent(This->handle_list[This->samples]);
1378 LeaveCriticalSection(&This->csList);
1379
1380 return S_OK;
1381 }
1382
1383 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1384 {
1385 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1386 int x;
1387
1388 TRACE("()\n");
1389
1390 EnterCriticalSection(&This->csList);
1391 ResetEvent(This->handle_list[This->samples]);
1392 This->bFlushing = FALSE;
1393 for (x = 0; x < This->samples; ++x)
1394 assert(!This->sample_list[x].pSample);
1395
1396 LeaveCriticalSection(&This->csList);
1397
1398 return S_OK;
1399 }
1400
1401 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1402 {
1403 FileAsyncReader_QueryInterface,
1404 FileAsyncReader_AddRef,
1405 FileAsyncReader_Release,
1406 FileAsyncReader_RequestAllocator,
1407 FileAsyncReader_Request,
1408 FileAsyncReader_WaitForNext,
1409 FileAsyncReader_SyncReadAligned,
1410 FileAsyncReader_SyncRead,
1411 FileAsyncReader_Length,
1412 FileAsyncReader_BeginFlush,
1413 FileAsyncReader_EndFlush,
1414 };
1415
1416
1417 static HRESULT WINAPI AMFilterMiscFlags_QueryInterface(IAMFilterMiscFlags *iface, REFIID riid, void **ppv) {
1418 AsyncReader *This = impl_from_IAMFilterMiscFlags(iface);
1419 return IUnknown_QueryInterface((IUnknown*)This, riid, ppv);
1420 }
1421
1422 static ULONG WINAPI AMFilterMiscFlags_AddRef(IAMFilterMiscFlags *iface) {
1423 AsyncReader *This = impl_from_IAMFilterMiscFlags(iface);
1424 return IUnknown_AddRef((IUnknown*)This);
1425 }
1426
1427 static ULONG WINAPI AMFilterMiscFlags_Release(IAMFilterMiscFlags *iface) {
1428 AsyncReader *This = impl_from_IAMFilterMiscFlags(iface);
1429 return IUnknown_Release((IUnknown*)This);
1430 }
1431
1432 static ULONG WINAPI AMFilterMiscFlags_GetMiscFlags(IAMFilterMiscFlags *iface) {
1433 return AM_FILTER_MISC_FLAGS_IS_SOURCE;
1434 }
1435
1436 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl = {
1437 AMFilterMiscFlags_QueryInterface,
1438 AMFilterMiscFlags_AddRef,
1439 AMFilterMiscFlags_Release,
1440 AMFilterMiscFlags_GetMiscFlags
1441 };