[QUARTZ][STRMBASE]
[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 = InterlockedDecrement(&This->filter.refCount);
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 BaseFilter_Destroy(&This->filter);
489 CoTaskMemFree(This);
490 return 0;
491 }
492 else
493 return refCount;
494 }
495
496 /** IMediaFilter methods **/
497
498 static HRESULT WINAPI AsyncReader_Stop(IBaseFilter * iface)
499 {
500 AsyncReader *This = impl_from_IBaseFilter(iface);
501
502 TRACE("()\n");
503
504 This->filter.state = State_Stopped;
505
506 return S_OK;
507 }
508
509 static HRESULT WINAPI AsyncReader_Pause(IBaseFilter * iface)
510 {
511 AsyncReader *This = impl_from_IBaseFilter(iface);
512
513 TRACE("()\n");
514
515 This->filter.state = State_Paused;
516
517 return S_OK;
518 }
519
520 static HRESULT WINAPI AsyncReader_Run(IBaseFilter * iface, REFERENCE_TIME tStart)
521 {
522 AsyncReader *This = impl_from_IBaseFilter(iface);
523
524 TRACE("(%x%08x)\n", (ULONG)(tStart >> 32), (ULONG)tStart);
525
526 This->filter.state = State_Running;
527
528 return S_OK;
529 }
530
531 /** IBaseFilter methods **/
532
533 static HRESULT WINAPI AsyncReader_FindPin(IBaseFilter * iface, LPCWSTR Id, IPin **ppPin)
534 {
535 AsyncReader *This = impl_from_IBaseFilter(iface);
536 TRACE("(%s, %p)\n", debugstr_w(Id), ppPin);
537
538 if (!Id || !ppPin)
539 return E_POINTER;
540
541 if (strcmpW(Id, wszOutputPinName))
542 {
543 *ppPin = NULL;
544 return VFW_E_NOT_FOUND;
545 }
546
547 *ppPin = This->pOutputPin;
548 IPin_AddRef(*ppPin);
549 return S_OK;
550 }
551
552 static const IBaseFilterVtbl AsyncReader_Vtbl =
553 {
554 AsyncReader_QueryInterface,
555 BaseFilterImpl_AddRef,
556 AsyncReader_Release,
557 BaseFilterImpl_GetClassID,
558 AsyncReader_Stop,
559 AsyncReader_Pause,
560 AsyncReader_Run,
561 BaseFilterImpl_GetState,
562 BaseFilterImpl_SetSyncSource,
563 BaseFilterImpl_GetSyncSource,
564 BaseFilterImpl_EnumPins,
565 AsyncReader_FindPin,
566 BaseFilterImpl_QueryFilterInfo,
567 BaseFilterImpl_JoinFilterGraph,
568 BaseFilterImpl_QueryVendorInfo
569 };
570
571 static HRESULT WINAPI FileSource_QueryInterface(IFileSourceFilter * iface, REFIID riid, LPVOID * ppv)
572 {
573 AsyncReader *This = impl_from_IFileSourceFilter(iface);
574
575 return IBaseFilter_QueryInterface(&This->filter.IBaseFilter_iface, riid, ppv);
576 }
577
578 static ULONG WINAPI FileSource_AddRef(IFileSourceFilter * iface)
579 {
580 AsyncReader *This = impl_from_IFileSourceFilter(iface);
581
582 return IBaseFilter_AddRef(&This->filter.IBaseFilter_iface);
583 }
584
585 static ULONG WINAPI FileSource_Release(IFileSourceFilter * iface)
586 {
587 AsyncReader *This = impl_from_IFileSourceFilter(iface);
588
589 return IBaseFilter_Release(&This->filter.IBaseFilter_iface);
590 }
591
592 static HRESULT WINAPI FileSource_Load(IFileSourceFilter * iface, LPCOLESTR pszFileName, const AM_MEDIA_TYPE * pmt)
593 {
594 HRESULT hr;
595 HANDLE hFile;
596 IAsyncReader * pReader = NULL;
597 AsyncReader *This = impl_from_IFileSourceFilter(iface);
598
599 TRACE("(%s, %p)\n", debugstr_w(pszFileName), pmt);
600
601 /* open file */
602 /* FIXME: check the sharing values that native uses */
603 hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
604
605 if (hFile == INVALID_HANDLE_VALUE)
606 {
607 return HRESULT_FROM_WIN32(GetLastError());
608 }
609
610 /* create pin */
611 hr = FileAsyncReader_Construct(hFile, &This->filter.IBaseFilter_iface, &This->filter.csFilter, &This->pOutputPin);
612 BaseFilterImpl_IncrementPinVersion(&This->filter);
613
614 if (SUCCEEDED(hr))
615 hr = IPin_QueryInterface(This->pOutputPin, &IID_IAsyncReader, (LPVOID *)&pReader);
616
617 /* store file name & media type */
618 if (SUCCEEDED(hr))
619 {
620 CoTaskMemFree(This->pszFileName);
621 if (This->pmt)
622 FreeMediaType(This->pmt);
623
624 This->pszFileName = CoTaskMemAlloc((strlenW(pszFileName) + 1) * sizeof(WCHAR));
625 strcpyW(This->pszFileName, pszFileName);
626
627 This->pmt = CoTaskMemAlloc(sizeof(AM_MEDIA_TYPE));
628 if (!pmt)
629 {
630 This->pmt->bFixedSizeSamples = TRUE;
631 This->pmt->bTemporalCompression = FALSE;
632 This->pmt->cbFormat = 0;
633 This->pmt->pbFormat = NULL;
634 This->pmt->pUnk = NULL;
635 This->pmt->lSampleSize = 0;
636 This->pmt->formattype = FORMAT_None;
637 hr = GetClassMediaFile(pReader, pszFileName, &This->pmt->majortype, &This->pmt->subtype, NULL);
638 if (FAILED(hr))
639 {
640 CoTaskMemFree(This->pmt);
641 This->pmt = NULL;
642 }
643 }
644 else
645 CopyMediaType(This->pmt, pmt);
646 }
647
648 if (pReader)
649 IAsyncReader_Release(pReader);
650
651 if (FAILED(hr))
652 {
653 if (This->pOutputPin)
654 {
655 IPin_Release(This->pOutputPin);
656 This->pOutputPin = NULL;
657 }
658
659 CoTaskMemFree(This->pszFileName);
660 if (This->pmt)
661 FreeMediaType(This->pmt);
662 This->pszFileName = NULL;
663 This->pmt = NULL;
664
665 CloseHandle(hFile);
666 }
667
668 /* FIXME: check return codes */
669 return hr;
670 }
671
672 static HRESULT WINAPI FileSource_GetCurFile(IFileSourceFilter * iface, LPOLESTR * ppszFileName, AM_MEDIA_TYPE * pmt)
673 {
674 AsyncReader *This = impl_from_IFileSourceFilter(iface);
675
676 TRACE("(%p, %p)\n", ppszFileName, pmt);
677
678 if (!ppszFileName)
679 return E_POINTER;
680
681 /* copy file name & media type if available, otherwise clear the outputs */
682 if (This->pszFileName)
683 {
684 *ppszFileName = CoTaskMemAlloc((strlenW(This->pszFileName) + 1) * sizeof(WCHAR));
685 strcpyW(*ppszFileName, This->pszFileName);
686 }
687 else
688 *ppszFileName = NULL;
689
690 if (pmt)
691 {
692 if (This->pmt)
693 CopyMediaType(pmt, This->pmt);
694 else
695 ZeroMemory(pmt, sizeof(*pmt));
696 }
697
698 return S_OK;
699 }
700
701 static const IFileSourceFilterVtbl FileSource_Vtbl =
702 {
703 FileSource_QueryInterface,
704 FileSource_AddRef,
705 FileSource_Release,
706 FileSource_Load,
707 FileSource_GetCurFile
708 };
709
710
711 /* the dwUserData passed back to user */
712 typedef struct DATAREQUEST
713 {
714 IMediaSample * pSample; /* sample passed to us by user */
715 DWORD_PTR dwUserData; /* user data passed to us */
716 OVERLAPPED ovl; /* our overlapped structure */
717 } DATAREQUEST;
718
719 typedef struct FileAsyncReader
720 {
721 BaseOutputPin pin;
722 IAsyncReader IAsyncReader_iface;
723
724 ALLOCATOR_PROPERTIES allocProps;
725 HANDLE hFile;
726 BOOL bFlushing;
727 /* Why would you need more? Every sample has its own handle */
728 LONG queued_number;
729 LONG samples;
730 LONG oldest_sample;
731 CRITICAL_SECTION csList; /* critical section to prevent concurrency issues */
732 DATAREQUEST *sample_list;
733
734 /* Have a handle for every sample, and then one more as flushing handle */
735 HANDLE *handle_list;
736 } FileAsyncReader;
737
738 static inline FileAsyncReader *impl_from_IPin(IPin *iface)
739 {
740 return CONTAINING_RECORD(iface, FileAsyncReader, pin.pin.IPin_iface);
741 }
742
743 static inline FileAsyncReader *impl_from_BasePin(BasePin *iface)
744 {
745 return CONTAINING_RECORD(iface, FileAsyncReader, pin.pin);
746 }
747
748 static inline FileAsyncReader *impl_from_BaseOutputPin(BaseOutputPin *iface)
749 {
750 return CONTAINING_RECORD(iface, FileAsyncReader, pin);
751 }
752
753 static inline BaseOutputPin *impl_BaseOututPin_from_BasePin(BasePin *iface)
754 {
755 return CONTAINING_RECORD(iface, BaseOutputPin, pin);
756 }
757
758 static inline FileAsyncReader *impl_from_IAsyncReader(IAsyncReader *iface)
759 {
760 return CONTAINING_RECORD(iface, FileAsyncReader, IAsyncReader_iface);
761 }
762
763 static HRESULT WINAPI FileAsyncReaderPin_QueryAccept(IPin *iface, const AM_MEDIA_TYPE *pmt)
764 {
765 FileAsyncReader *This = impl_from_IPin(iface);
766 AM_MEDIA_TYPE *pmt_filter = impl_from_IBaseFilter(This->pin.pin.pinInfo.pFilter)->pmt;
767
768 FIXME("(%p, %p)\n", iface, pmt);
769
770 if (IsEqualGUID(&pmt->majortype, &pmt_filter->majortype) &&
771 IsEqualGUID(&pmt->subtype, &pmt_filter->subtype) &&
772 IsEqualGUID(&pmt->formattype, &FORMAT_None))
773 return S_OK;
774
775 return S_FALSE;
776 }
777
778 static HRESULT WINAPI FileAsyncReaderPin_GetMediaType(BasePin *iface, int iPosition, AM_MEDIA_TYPE *pmt)
779 {
780 FileAsyncReader *This = impl_from_BasePin(iface);
781 if (iPosition < 0)
782 return E_INVALIDARG;
783 if (iPosition > 0)
784 return VFW_S_NO_MORE_ITEMS;
785 CopyMediaType(pmt, impl_from_IBaseFilter(This->pin.pin.pinInfo.pFilter)->pmt);
786 return S_OK;
787 }
788
789 /* overridden pin functions */
790
791 static HRESULT WINAPI FileAsyncReaderPin_QueryInterface(IPin * iface, REFIID riid, LPVOID * ppv)
792 {
793 FileAsyncReader *This = impl_from_IPin(iface);
794 TRACE("(%s, %p)\n", qzdebugstr_guid(riid), ppv);
795
796 *ppv = NULL;
797
798 if (IsEqualIID(riid, &IID_IUnknown))
799 *ppv = This;
800 else if (IsEqualIID(riid, &IID_IPin))
801 *ppv = This;
802 else if (IsEqualIID(riid, &IID_IAsyncReader))
803 *ppv = &This->IAsyncReader_iface;
804
805 if (*ppv)
806 {
807 IUnknown_AddRef((IUnknown *)(*ppv));
808 return S_OK;
809 }
810
811 if (!IsEqualIID(riid, &IID_IPin) && !IsEqualIID(riid, &IID_IMediaSeeking))
812 FIXME("No interface for %s!\n", qzdebugstr_guid(riid));
813
814 return E_NOINTERFACE;
815 }
816
817 static ULONG WINAPI FileAsyncReaderPin_Release(IPin * iface)
818 {
819 FileAsyncReader *This = impl_from_IPin(iface);
820 ULONG refCount = InterlockedDecrement(&This->pin.pin.refCount);
821 int x;
822
823 TRACE("(%p)->() Release from %d\n", This, refCount + 1);
824
825 if (!refCount)
826 {
827 CoTaskMemFree(This->sample_list);
828 if (This->handle_list)
829 {
830 for (x = 0; x <= This->samples; ++x)
831 CloseHandle(This->handle_list[x]);
832 CoTaskMemFree(This->handle_list);
833 }
834 CloseHandle(This->hFile);
835 This->csList.DebugInfo->Spare[0] = 0;
836 DeleteCriticalSection(&This->csList);
837 CoTaskMemFree(This);
838 return 0;
839 }
840 return refCount;
841 }
842
843 static const IPinVtbl FileAsyncReaderPin_Vtbl =
844 {
845 FileAsyncReaderPin_QueryInterface,
846 BasePinImpl_AddRef,
847 FileAsyncReaderPin_Release,
848 BaseOutputPinImpl_Connect,
849 BaseOutputPinImpl_ReceiveConnection,
850 BasePinImpl_Disconnect,
851 BasePinImpl_ConnectedTo,
852 BasePinImpl_ConnectionMediaType,
853 BasePinImpl_QueryPinInfo,
854 BasePinImpl_QueryDirection,
855 BasePinImpl_QueryId,
856 FileAsyncReaderPin_QueryAccept,
857 BasePinImpl_EnumMediaTypes,
858 BasePinImpl_QueryInternalConnections,
859 BaseOutputPinImpl_EndOfStream,
860 BaseOutputPinImpl_BeginFlush,
861 BaseOutputPinImpl_EndFlush,
862 BasePinImpl_NewSegment
863 };
864
865 /* Function called as a helper to IPin_Connect */
866 /* specific AM_MEDIA_TYPE - it cannot be NULL */
867 /* this differs from standard OutputPin_AttemptConnection only in that it
868 * doesn't need the IMemInputPin interface on the receiving pin */
869 static HRESULT WINAPI FileAsyncReaderPin_AttemptConnection(BasePin * iface, IPin * pReceivePin, const AM_MEDIA_TYPE * pmt)
870 {
871 BaseOutputPin *This = impl_BaseOututPin_from_BasePin(iface);
872 HRESULT hr;
873
874 TRACE("(%p, %p)\n", pReceivePin, pmt);
875 dump_AM_MEDIA_TYPE(pmt);
876
877 /* FIXME: call queryacceptproc */
878
879 This->pin.pConnectedTo = pReceivePin;
880 IPin_AddRef(pReceivePin);
881 CopyMediaType(&This->pin.mtCurrent, pmt);
882
883 hr = IPin_ReceiveConnection(pReceivePin, &iface->IPin_iface, pmt);
884
885 if (FAILED(hr))
886 {
887 IPin_Release(This->pin.pConnectedTo);
888 This->pin.pConnectedTo = NULL;
889 FreeMediaType(&This->pin.mtCurrent);
890 }
891
892 TRACE(" -- %x\n", hr);
893 return hr;
894 }
895
896 static HRESULT WINAPI FileAsyncReaderPin_DecideBufferSize(BaseOutputPin *iface, IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *ppropInputRequest)
897 {
898 FileAsyncReader *This = impl_from_BaseOutputPin(iface);
899 ALLOCATOR_PROPERTIES actual;
900
901 if (ppropInputRequest->cbAlign && ppropInputRequest->cbAlign != This->allocProps.cbAlign)
902 FIXME("Requested Buffer cbAlign mismatch %i,%i\n",This->allocProps.cbAlign, ppropInputRequest->cbAlign);
903 if (ppropInputRequest->cbPrefix)
904 FIXME("Requested Buffer cbPrefix mismatch %i,%i\n",This->allocProps.cbPrefix, ppropInputRequest->cbPrefix);
905 if (ppropInputRequest->cbBuffer)
906 FIXME("Requested Buffer cbBuffer mismatch %i,%i\n",This->allocProps.cbBuffer, ppropInputRequest->cbBuffer);
907 if (ppropInputRequest->cBuffers)
908 FIXME("Requested Buffer cBuffers mismatch %i,%i\n",This->allocProps.cBuffers, ppropInputRequest->cBuffers);
909
910 return IMemAllocator_SetProperties(pAlloc, &This->allocProps, &actual);
911 }
912
913 static const BaseOutputPinFuncTable output_BaseOutputFuncTable = {
914 {
915 NULL,
916 FileAsyncReaderPin_AttemptConnection,
917 BasePinImpl_GetMediaTypeVersion,
918 FileAsyncReaderPin_GetMediaType
919 },
920 FileAsyncReaderPin_DecideBufferSize,
921 BaseOutputPinImpl_DecideAllocator,
922 BaseOutputPinImpl_BreakConnect
923 };
924
925 static HRESULT FileAsyncReader_Construct(HANDLE hFile, IBaseFilter * pBaseFilter, LPCRITICAL_SECTION pCritSec, IPin ** ppPin)
926 {
927 PIN_INFO piOutput;
928 HRESULT hr;
929
930 *ppPin = NULL;
931 piOutput.dir = PINDIR_OUTPUT;
932 piOutput.pFilter = pBaseFilter;
933 strcpyW(piOutput.achName, wszOutputPinName);
934 hr = BaseOutputPin_Construct(&FileAsyncReaderPin_Vtbl, sizeof(FileAsyncReader), &piOutput, &output_BaseOutputFuncTable, pCritSec, ppPin);
935
936 if (SUCCEEDED(hr))
937 {
938 FileAsyncReader *pPinImpl = (FileAsyncReader *)*ppPin;
939 pPinImpl->IAsyncReader_iface.lpVtbl = &FileAsyncReader_Vtbl;
940 pPinImpl->hFile = hFile;
941 pPinImpl->bFlushing = FALSE;
942 pPinImpl->sample_list = NULL;
943 pPinImpl->handle_list = NULL;
944 pPinImpl->queued_number = 0;
945 InitializeCriticalSection(&pPinImpl->csList);
946 pPinImpl->csList.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": FileAsyncReader.csList");
947 }
948 return hr;
949 }
950
951 /* IAsyncReader */
952
953 static HRESULT WINAPI FileAsyncReader_QueryInterface(IAsyncReader * iface, REFIID riid, LPVOID * ppv)
954 {
955 FileAsyncReader *This = impl_from_IAsyncReader(iface);
956
957 return IPin_QueryInterface(&This->pin.pin.IPin_iface, riid, ppv);
958 }
959
960 static ULONG WINAPI FileAsyncReader_AddRef(IAsyncReader * iface)
961 {
962 FileAsyncReader *This = impl_from_IAsyncReader(iface);
963
964 return IPin_AddRef(&This->pin.pin.IPin_iface);
965 }
966
967 static ULONG WINAPI FileAsyncReader_Release(IAsyncReader * iface)
968 {
969 FileAsyncReader *This = impl_from_IAsyncReader(iface);
970
971 return IPin_Release(&This->pin.pin.IPin_iface);
972 }
973
974 #define DEF_ALIGNMENT 1
975
976 static HRESULT WINAPI FileAsyncReader_RequestAllocator(IAsyncReader * iface, IMemAllocator * pPreferred, ALLOCATOR_PROPERTIES * pProps, IMemAllocator ** ppActual)
977 {
978 FileAsyncReader *This = impl_from_IAsyncReader(iface);
979
980 HRESULT hr = S_OK;
981
982 TRACE("(%p, %p, %p)\n", pPreferred, pProps, ppActual);
983
984 if (!pProps->cbAlign || (pProps->cbAlign % DEF_ALIGNMENT) != 0)
985 pProps->cbAlign = DEF_ALIGNMENT;
986
987 if (pPreferred)
988 {
989 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
990 /* FIXME: check we are still aligned */
991 if (SUCCEEDED(hr))
992 {
993 IMemAllocator_AddRef(pPreferred);
994 *ppActual = pPreferred;
995 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
996 goto done;
997 }
998 }
999
1000 pPreferred = NULL;
1001
1002 hr = CoCreateInstance(&CLSID_MemoryAllocator, NULL, CLSCTX_INPROC, &IID_IMemAllocator, (LPVOID *)&pPreferred);
1003
1004 if (SUCCEEDED(hr))
1005 {
1006 hr = IMemAllocator_SetProperties(pPreferred, pProps, pProps);
1007 /* FIXME: check we are still aligned */
1008 if (SUCCEEDED(hr))
1009 {
1010 *ppActual = pPreferred;
1011 TRACE("FileAsyncReader_RequestAllocator -- %x\n", hr);
1012 }
1013 }
1014
1015 done:
1016 if (SUCCEEDED(hr))
1017 {
1018 CoTaskMemFree(This->sample_list);
1019 if (This->handle_list)
1020 {
1021 int x;
1022 for (x = 0; x <= This->samples; ++x)
1023 CloseHandle(This->handle_list[x]);
1024 CoTaskMemFree(This->handle_list);
1025 }
1026
1027 This->samples = pProps->cBuffers;
1028 This->oldest_sample = 0;
1029 TRACE("Samples: %u\n", This->samples);
1030 This->sample_list = CoTaskMemAlloc(sizeof(This->sample_list[0]) * pProps->cBuffers);
1031 This->handle_list = CoTaskMemAlloc(sizeof(HANDLE) * pProps->cBuffers * 2);
1032
1033 if (This->sample_list && This->handle_list)
1034 {
1035 int x;
1036 ZeroMemory(This->sample_list, sizeof(This->sample_list[0]) * pProps->cBuffers);
1037 for (x = 0; x < This->samples; ++x)
1038 {
1039 This->sample_list[x].ovl.hEvent = This->handle_list[x] = CreateEventW(NULL, 0, 0, NULL);
1040 if (x + 1 < This->samples)
1041 This->handle_list[This->samples + 1 + x] = This->handle_list[x];
1042 }
1043 This->handle_list[This->samples] = CreateEventW(NULL, 1, 0, NULL);
1044 This->allocProps = *pProps;
1045 }
1046 else
1047 {
1048 hr = E_OUTOFMEMORY;
1049 CoTaskMemFree(This->sample_list);
1050 CoTaskMemFree(This->handle_list);
1051 This->samples = 0;
1052 This->sample_list = NULL;
1053 This->handle_list = NULL;
1054 }
1055 }
1056
1057 if (FAILED(hr))
1058 {
1059 *ppActual = NULL;
1060 if (pPreferred)
1061 IMemAllocator_Release(pPreferred);
1062 }
1063
1064 TRACE("-- %x\n", hr);
1065 return hr;
1066 }
1067
1068 /* we could improve the Request/WaitForNext mechanism by allowing out of order samples.
1069 * however, this would be quite complicated to do and may be a bit error prone */
1070 static HRESULT WINAPI FileAsyncReader_Request(IAsyncReader * iface, IMediaSample * pSample, DWORD_PTR dwUser)
1071 {
1072 HRESULT hr = S_OK;
1073 REFERENCE_TIME Start;
1074 REFERENCE_TIME Stop;
1075 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1076 LPBYTE pBuffer = NULL;
1077
1078 TRACE("(%p, %lx)\n", pSample, dwUser);
1079
1080 if (!pSample)
1081 return E_POINTER;
1082
1083 /* get start and stop positions in bytes */
1084 if (SUCCEEDED(hr))
1085 hr = IMediaSample_GetTime(pSample, &Start, &Stop);
1086
1087 if (SUCCEEDED(hr))
1088 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1089
1090 EnterCriticalSection(&This->csList);
1091 if (This->bFlushing)
1092 {
1093 LeaveCriticalSection(&This->csList);
1094 return VFW_E_WRONG_STATE;
1095 }
1096
1097 if (SUCCEEDED(hr))
1098 {
1099 DWORD dwLength = (DWORD) BYTES_FROM_MEDIATIME(Stop - Start);
1100 DATAREQUEST *pDataRq;
1101 int x;
1102
1103 /* Try to insert above the waiting sample if possible */
1104 for (x = This->oldest_sample; x < This->samples; ++x)
1105 {
1106 if (!This->sample_list[x].pSample)
1107 break;
1108 }
1109
1110 if (x >= This->samples)
1111 for (x = 0; x < This->oldest_sample; ++x)
1112 {
1113 if (!This->sample_list[x].pSample)
1114 break;
1115 }
1116
1117 /* There must be a sample we have found */
1118 assert(x < This->samples);
1119 ++This->queued_number;
1120
1121 pDataRq = This->sample_list + x;
1122
1123 pDataRq->ovl.u.s.Offset = (DWORD) BYTES_FROM_MEDIATIME(Start);
1124 pDataRq->ovl.u.s.OffsetHigh = (DWORD)(BYTES_FROM_MEDIATIME(Start) >> (sizeof(DWORD) * 8));
1125 pDataRq->dwUserData = dwUser;
1126
1127 /* we violate traditional COM rules here by maintaining
1128 * a reference to the sample, but not calling AddRef, but
1129 * that's what MSDN says to do */
1130 pDataRq->pSample = pSample;
1131
1132 /* this is definitely not how it is implemented on Win9x
1133 * as they do not support async reads on files, but it is
1134 * sooo much easier to use this than messing around with threads!
1135 */
1136 if (!ReadFile(This->hFile, pBuffer, dwLength, NULL, &pDataRq->ovl))
1137 hr = HRESULT_FROM_WIN32(GetLastError());
1138
1139 /* ERROR_IO_PENDING is not actually an error since this is what we want! */
1140 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1141 hr = S_OK;
1142 }
1143
1144 LeaveCriticalSection(&This->csList);
1145
1146 TRACE("-- %x\n", hr);
1147 return hr;
1148 }
1149
1150 static HRESULT WINAPI FileAsyncReader_WaitForNext(IAsyncReader * iface, DWORD dwTimeout, IMediaSample ** ppSample, DWORD_PTR * pdwUser)
1151 {
1152 HRESULT hr = S_OK;
1153 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1154 DWORD buffer = ~0;
1155
1156 TRACE("(%u, %p, %p)\n", dwTimeout, ppSample, pdwUser);
1157
1158 *ppSample = NULL;
1159 *pdwUser = 0;
1160
1161 EnterCriticalSection(&This->csList);
1162 if (!This->bFlushing)
1163 {
1164 LONG oldest = This->oldest_sample;
1165
1166 if (!This->queued_number)
1167 {
1168 /* It could be that nothing is queued right now, but that can be fixed */
1169 WARN("Called without samples in queue and not flushing!!\n");
1170 }
1171 LeaveCriticalSection(&This->csList);
1172
1173 /* wait for an object to read, or time out */
1174 buffer = WaitForMultipleObjectsEx(This->samples+1, This->handle_list + oldest, FALSE, dwTimeout, TRUE);
1175
1176 EnterCriticalSection(&This->csList);
1177 if (buffer <= This->samples)
1178 {
1179 /* Re-scale the buffer back to normal */
1180 buffer += oldest;
1181
1182 /* Uh oh, we overshot the flusher handle, renormalize it back to 0..Samples-1 */
1183 if (buffer > This->samples)
1184 buffer -= This->samples + 1;
1185 assert(buffer <= This->samples);
1186 }
1187
1188 if (buffer >= This->samples)
1189 {
1190 if (buffer != This->samples)
1191 {
1192 FIXME("Returned: %u (%08x)\n", buffer, GetLastError());
1193 hr = VFW_E_TIMEOUT;
1194 }
1195 else
1196 hr = VFW_E_WRONG_STATE;
1197 buffer = ~0;
1198 }
1199 else
1200 --This->queued_number;
1201 }
1202
1203 if (This->bFlushing && buffer == ~0)
1204 {
1205 for (buffer = 0; buffer < This->samples; ++buffer)
1206 {
1207 if (This->sample_list[buffer].pSample)
1208 {
1209 ResetEvent(This->handle_list[buffer]);
1210 break;
1211 }
1212 }
1213 if (buffer == This->samples)
1214 {
1215 assert(!This->queued_number);
1216 hr = VFW_E_TIMEOUT;
1217 }
1218 else
1219 {
1220 --This->queued_number;
1221 hr = S_OK;
1222 }
1223 }
1224
1225 if (SUCCEEDED(hr))
1226 {
1227 REFERENCE_TIME rtStart, rtStop;
1228 REFERENCE_TIME rtSampleStart, rtSampleStop;
1229 DATAREQUEST *pDataRq = This->sample_list + buffer;
1230 DWORD dwBytes = 0;
1231
1232 /* get any errors */
1233 if (!This->bFlushing && !GetOverlappedResult(This->hFile, &pDataRq->ovl, &dwBytes, FALSE))
1234 hr = HRESULT_FROM_WIN32(GetLastError());
1235
1236 /* Return the sample no matter what so it can be destroyed */
1237 *ppSample = pDataRq->pSample;
1238 *pdwUser = pDataRq->dwUserData;
1239
1240 if (This->bFlushing)
1241 hr = VFW_E_WRONG_STATE;
1242
1243 if (FAILED(hr))
1244 dwBytes = 0;
1245
1246 /* Set the time on the sample */
1247 IMediaSample_SetActualDataLength(pDataRq->pSample, dwBytes);
1248
1249 rtStart = (DWORD64)pDataRq->ovl.u.s.Offset + ((DWORD64)pDataRq->ovl.u.s.OffsetHigh << 32);
1250 rtStart = MEDIATIME_FROM_BYTES(rtStart);
1251 rtStop = rtStart + MEDIATIME_FROM_BYTES(dwBytes);
1252
1253 IMediaSample_GetTime(pDataRq->pSample, &rtSampleStart, &rtSampleStop);
1254 assert(rtStart == rtSampleStart);
1255 assert(rtStop <= rtSampleStop);
1256
1257 IMediaSample_SetTime(pDataRq->pSample, &rtStart, &rtStop);
1258 assert(rtStart == rtSampleStart);
1259 if (hr == S_OK)
1260 assert(rtStop == rtSampleStop);
1261 else
1262 assert(rtStop == rtStart);
1263
1264 This->sample_list[buffer].pSample = NULL;
1265 assert(This->oldest_sample < This->samples);
1266
1267 if (buffer == This->oldest_sample)
1268 {
1269 LONG x;
1270 for (x = This->oldest_sample + 1; x < This->samples; ++x)
1271 if (This->sample_list[x].pSample)
1272 break;
1273 if (x >= This->samples)
1274 for (x = 0; x < This->oldest_sample; ++x)
1275 if (This->sample_list[x].pSample)
1276 break;
1277 if (This->oldest_sample == x)
1278 /* No samples found, reset to 0 */
1279 x = 0;
1280 This->oldest_sample = x;
1281 }
1282 }
1283 LeaveCriticalSection(&This->csList);
1284
1285 TRACE("-- %x\n", hr);
1286 return hr;
1287 }
1288
1289 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer);
1290
1291 static HRESULT WINAPI FileAsyncReader_SyncReadAligned(IAsyncReader * iface, IMediaSample * pSample)
1292 {
1293 BYTE * pBuffer;
1294 REFERENCE_TIME tStart;
1295 REFERENCE_TIME tStop;
1296 HRESULT hr;
1297
1298 TRACE("(%p)\n", pSample);
1299
1300 hr = IMediaSample_GetTime(pSample, &tStart, &tStop);
1301
1302 if (SUCCEEDED(hr))
1303 hr = IMediaSample_GetPointer(pSample, &pBuffer);
1304
1305 if (SUCCEEDED(hr))
1306 hr = FileAsyncReader_SyncRead(iface,
1307 BYTES_FROM_MEDIATIME(tStart),
1308 (LONG) BYTES_FROM_MEDIATIME(tStop - tStart),
1309 pBuffer);
1310
1311 TRACE("-- %x\n", hr);
1312 return hr;
1313 }
1314
1315 static HRESULT WINAPI FileAsyncReader_SyncRead(IAsyncReader * iface, LONGLONG llPosition, LONG lLength, BYTE * pBuffer)
1316 {
1317 OVERLAPPED ovl;
1318 HRESULT hr = S_OK;
1319 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1320
1321 TRACE("(%x%08x, %d, %p)\n", (ULONG)(llPosition >> 32), (ULONG)llPosition, lLength, pBuffer);
1322
1323 ZeroMemory(&ovl, sizeof(ovl));
1324
1325 ovl.hEvent = CreateEventW(NULL, 0, 0, NULL);
1326 /* NOTE: llPosition is the actual byte position to start reading from */
1327 ovl.u.s.Offset = (DWORD) llPosition;
1328 ovl.u.s.OffsetHigh = (DWORD) (llPosition >> (sizeof(DWORD) * 8));
1329
1330 if (!ReadFile(This->hFile, pBuffer, lLength, NULL, &ovl))
1331 hr = HRESULT_FROM_WIN32(GetLastError());
1332
1333 if (hr == HRESULT_FROM_WIN32(ERROR_IO_PENDING))
1334 hr = S_OK;
1335
1336 if (SUCCEEDED(hr))
1337 {
1338 DWORD dwBytesRead;
1339
1340 if (!GetOverlappedResult(This->hFile, &ovl, &dwBytesRead, TRUE))
1341 hr = HRESULT_FROM_WIN32(GetLastError());
1342 }
1343
1344 CloseHandle(ovl.hEvent);
1345
1346 TRACE("-- %x\n", hr);
1347 return hr;
1348 }
1349
1350 static HRESULT WINAPI FileAsyncReader_Length(IAsyncReader * iface, LONGLONG * pTotal, LONGLONG * pAvailable)
1351 {
1352 DWORD dwSizeLow;
1353 DWORD dwSizeHigh;
1354 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1355
1356 TRACE("(%p, %p)\n", pTotal, pAvailable);
1357
1358 if (((dwSizeLow = GetFileSize(This->hFile, &dwSizeHigh)) == -1) &&
1359 (GetLastError() != NO_ERROR))
1360 return HRESULT_FROM_WIN32(GetLastError());
1361
1362 *pTotal = (LONGLONG)dwSizeLow | (LONGLONG)dwSizeHigh << (sizeof(DWORD) * 8);
1363
1364 *pAvailable = *pTotal;
1365
1366 return S_OK;
1367 }
1368
1369 static HRESULT WINAPI FileAsyncReader_BeginFlush(IAsyncReader * iface)
1370 {
1371 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1372
1373 TRACE("()\n");
1374
1375 EnterCriticalSection(&This->csList);
1376 This->bFlushing = TRUE;
1377 CancelIo(This->hFile);
1378 SetEvent(This->handle_list[This->samples]);
1379 LeaveCriticalSection(&This->csList);
1380
1381 return S_OK;
1382 }
1383
1384 static HRESULT WINAPI FileAsyncReader_EndFlush(IAsyncReader * iface)
1385 {
1386 FileAsyncReader *This = impl_from_IAsyncReader(iface);
1387 int x;
1388
1389 TRACE("()\n");
1390
1391 EnterCriticalSection(&This->csList);
1392 ResetEvent(This->handle_list[This->samples]);
1393 This->bFlushing = FALSE;
1394 for (x = 0; x < This->samples; ++x)
1395 assert(!This->sample_list[x].pSample);
1396
1397 LeaveCriticalSection(&This->csList);
1398
1399 return S_OK;
1400 }
1401
1402 static const IAsyncReaderVtbl FileAsyncReader_Vtbl =
1403 {
1404 FileAsyncReader_QueryInterface,
1405 FileAsyncReader_AddRef,
1406 FileAsyncReader_Release,
1407 FileAsyncReader_RequestAllocator,
1408 FileAsyncReader_Request,
1409 FileAsyncReader_WaitForNext,
1410 FileAsyncReader_SyncReadAligned,
1411 FileAsyncReader_SyncRead,
1412 FileAsyncReader_Length,
1413 FileAsyncReader_BeginFlush,
1414 FileAsyncReader_EndFlush,
1415 };
1416
1417
1418 static HRESULT WINAPI AMFilterMiscFlags_QueryInterface(IAMFilterMiscFlags *iface, REFIID riid, void **ppv) {
1419 AsyncReader *This = impl_from_IAMFilterMiscFlags(iface);
1420 return IUnknown_QueryInterface((IUnknown*)This, riid, ppv);
1421 }
1422
1423 static ULONG WINAPI AMFilterMiscFlags_AddRef(IAMFilterMiscFlags *iface) {
1424 AsyncReader *This = impl_from_IAMFilterMiscFlags(iface);
1425 return IUnknown_AddRef((IUnknown*)This);
1426 }
1427
1428 static ULONG WINAPI AMFilterMiscFlags_Release(IAMFilterMiscFlags *iface) {
1429 AsyncReader *This = impl_from_IAMFilterMiscFlags(iface);
1430 return IUnknown_Release((IUnknown*)This);
1431 }
1432
1433 static ULONG WINAPI AMFilterMiscFlags_GetMiscFlags(IAMFilterMiscFlags *iface) {
1434 return AM_FILTER_MISC_FLAGS_IS_SOURCE;
1435 }
1436
1437 static const IAMFilterMiscFlagsVtbl IAMFilterMiscFlags_Vtbl = {
1438 AMFilterMiscFlags_QueryInterface,
1439 AMFilterMiscFlags_AddRef,
1440 AMFilterMiscFlags_Release,
1441 AMFilterMiscFlags_GetMiscFlags
1442 };