2 * Implementation of IFilterGraph and related interfaces
3 * + IGraphVersion, IGraphConfig
5 * FIXME - create a thread to process some methods correctly.
8 * FIXME - process Pause/Stop asynchronously and notify when completed.
11 * Copyright (C) Hidenori TAKESHIMA <hidenori@a2.ctktv.ne.jp>
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(quartz);
45 #include "quartz_private.h"
53 #define NUMELEMS(elem) (sizeof(elem)/sizeof(elem[0]))
56 static HRESULT CFilterGraph_DisconnectAllPins( IBaseFilter* pFilter )
58 IEnumPins* pEnum = NULL;
64 hr = IBaseFilter_EnumPins( pFilter, &pEnum );
74 hr = IEnumPins_Next( pEnum, 1, &pPin, &cFetched );
77 if ( hr != NOERROR || pPin == NULL || cFetched != 1 )
84 hr = IPin_ConnectedTo(pPin,&pConnTo);
85 if ( hr == NOERROR && pConnTo != NULL )
87 IPin_Disconnect(pPin);
88 IPin_Disconnect(pConnTo);
89 IPin_Release(pConnTo);
95 IEnumPins_Release( pEnum );
101 static HRESULT CFilterGraph_GraphChanged( CFilterGraph* This )
103 /* IDistributorNotify_NotifyGraphChange() */
105 IMediaEventSink_Notify(CFilterGraph_IMediaEventSink(This),
106 EC_GRAPH_CHANGED, 0, 0);
107 This->m_lGraphVersion ++;
112 /***************************************************************************
114 * CFilterGraph internal methods for IFilterGraph2::AddSourceFilter().
118 static HRESULT QUARTZ_PeekFile(
119 const WCHAR* pwszFileName,
120 BYTE* pData, DWORD cbData, DWORD* pcbRead )
126 hFile = CreateFileW( pwszFileName,
127 GENERIC_READ, FILE_SHARE_READ,
128 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, (HANDLE)NULL );
129 if ( hFile == INVALID_HANDLE_VALUE )
131 if ( ReadFile( hFile, pData, cbData, pcbRead, NULL ) )
133 CloseHandle( hFile );
139 static const WCHAR* skip_space(const WCHAR* pwsz)
141 if ( pwsz == NULL ) return NULL;
142 while ( *pwsz == (WCHAR)' ' ) pwsz++;
146 static const WCHAR* get_dword(const WCHAR* pwsz,DWORD* pdw)
151 if ( pwsz == NULL ) return NULL;
152 while ( *pwsz >= (WCHAR)'0' && *pwsz <= (WCHAR)'9' )
154 dw = dw * 10 + (DWORD)(*pwsz-(WCHAR)'0');
161 static int wchar_to_hex(WCHAR wch)
163 if ( wch >= (WCHAR)'0' && wch <= (WCHAR)'9' )
164 return (int)(wch - (WCHAR)'0');
165 if ( wch >= (WCHAR)'A' && wch <= (WCHAR)'F' )
166 return (int)(wch - (WCHAR)'A' + 10);
167 if ( wch >= (WCHAR)'a' && wch <= (WCHAR)'f' )
168 return (int)(wch - (WCHAR)'a' + 10);
172 static const WCHAR* get_hex(const WCHAR* pwsz,BYTE* pb)
177 if ( pwsz == NULL ) return NULL;
178 hi = wchar_to_hex(*pwsz); if ( hi < 0 ) return NULL; pwsz++;
179 lo = wchar_to_hex(*pwsz); if ( lo < 0 ) return NULL; pwsz++;
180 *pb = (BYTE)( (hi << 4) | lo );
184 static const WCHAR* skip_hex(const WCHAR* pwsz)
186 if ( pwsz == NULL ) return NULL;
189 if ( wchar_to_hex(*pwsz) < 0 )
196 static const WCHAR* next_token(const WCHAR* pwsz)
198 if ( pwsz == NULL ) return NULL;
199 pwsz = skip_space(pwsz);
200 if ( *pwsz != (WCHAR)',' ) return NULL; pwsz++;
201 return skip_space(pwsz);
205 static HRESULT QUARTZ_SourceTypeIsMatch(
206 const BYTE* pData, DWORD cbData,
207 const WCHAR* pwszTempl, DWORD cchTempl )
212 const WCHAR* pwszMask;
213 const WCHAR* pwszValue;
217 TRACE("(%p,%lu,%s,%lu)\n",pData,cbData,debugstr_w(pwszTempl),cchTempl);
219 pwszTempl = skip_space(pwszTempl);
222 pwszTempl = get_dword(pwszTempl,&dwOfs);
223 pwszTempl = next_token(pwszTempl);
224 pwszTempl = get_dword(pwszTempl,&cbLen);
225 pwszMask = pwszTempl = next_token(pwszTempl);
226 pwszTempl = skip_hex(pwszTempl);
227 pwszValue = pwszTempl = next_token(pwszTempl);
228 pwszTempl = skip_hex(pwszValue);
229 pwszTempl = skip_space(pwszTempl);
230 if ( pwszValue == NULL )
232 WARN( "parse error\n" );
236 if ( dwOfs >= cbData || ( (dwOfs+cbLen) >= cbData ) )
238 WARN( "length of given data is too short\n" );
242 for ( n = 0; n < cbLen; n++ )
244 pwszMask = get_hex(pwszMask,&bMask);
245 if ( pwszMask == NULL ) bMask = 0xff;
246 pwszValue = get_hex(pwszValue,&bValue);
247 if ( pwszValue == NULL )
249 WARN( "parse error - invalid hex data\n" );
252 if ( (pData[dwOfs+n]&bMask) != (bValue&bMask) )
254 TRACE( "not matched\n" );
259 if ( *pwszTempl == 0 )
261 pwszTempl = next_token(pwszTempl);
262 if ( pwszTempl == NULL )
264 WARN( "parse error\n" );
269 TRACE( "matched\n" );
273 static HRESULT QUARTZ_GetSourceTypeFromData(
274 const BYTE* pData, DWORD cbData,
275 GUID* pidMajor, GUID* pidSub, CLSID* pidSource )
277 HRESULT hr = S_FALSE;
281 WCHAR wszSource[128];
282 WCHAR wszSourceFilter[128];
283 WCHAR* pwszLocalBuf = NULL;
285 DWORD cbLocalBuf = 0;
295 FILETIME ftLastWrite;
296 static const WCHAR wszFmt[] = {'%','l','u',0};
298 if ( RegOpenKeyExW( HKEY_CLASSES_ROOT, QUARTZ_wszMediaType, 0, KEY_READ, &hkMajor ) == ERROR_SUCCESS )
301 while ( hr == S_FALSE )
303 cbPath = NUMELEMS(wszMajor)-1;
305 hkMajor, dwIndexMajor ++, wszMajor, &cbPath,
306 NULL, NULL, NULL, &ftLastWrite );
307 if ( lr != ERROR_SUCCESS )
309 if ( RegOpenKeyExW( hkMajor, wszMajor, 0, KEY_READ, &hkSub ) == ERROR_SUCCESS )
312 while ( hr == S_FALSE )
314 cbPath = NUMELEMS(wszSub)-1;
316 hkSub, dwIndexSub ++, wszSub, &cbPath,
317 NULL, NULL, NULL, &ftLastWrite );
318 if ( lr != ERROR_SUCCESS )
320 if ( RegOpenKeyExW( hkSub, wszSub, 0, KEY_READ, &hkSource ) == ERROR_SUCCESS )
323 while ( hr == S_FALSE )
325 wsprintfW(wszSource,wszFmt,dwIndexSource++);
326 lr = RegQueryValueExW(
327 hkSource, wszSource, NULL,
328 &dwRegType, NULL, &cbRegData );
329 if ( lr != ERROR_SUCCESS )
331 if ( cbLocalBuf < cbRegData )
333 pwszTemp = (WCHAR*)QUARTZ_ReallocMem( pwszLocalBuf, cbRegData+sizeof(WCHAR) );
334 if ( pwszTemp == NULL )
339 pwszLocalBuf = pwszTemp;
340 cbLocalBuf = cbRegData+sizeof(WCHAR);
342 cbRegData = cbLocalBuf;
343 lr = RegQueryValueExW(
344 hkSource, wszSource, NULL,
345 &dwRegType, (BYTE*)pwszLocalBuf, &cbRegData );
346 if ( lr != ERROR_SUCCESS )
349 hr = QUARTZ_SourceTypeIsMatch(
351 pwszLocalBuf, cbRegData / sizeof(WCHAR) );
354 hr = CLSIDFromString(wszMajor,pidMajor);
356 hr = CLSIDFromString(wszSub,pidSub);
359 lstrcpyW(wszSource,QUARTZ_wszSourceFilter);
360 cbRegData = NUMELEMS(wszSourceFilter)-sizeof(WCHAR);
361 lr = RegQueryValueExW(
362 hkSource, wszSource, NULL,
364 (BYTE*)wszSourceFilter, &cbRegData );
365 if ( lr == ERROR_SUCCESS )
367 hr = CLSIDFromString(wszSourceFilter,pidSource);
373 if ( hr != NOERROR && SUCCEEDED(hr) )
379 RegCloseKey( hkSource );
382 RegCloseKey( hkSub );
385 RegCloseKey( hkMajor );
388 if ( pwszLocalBuf != NULL )
389 QUARTZ_FreeMem(pwszLocalBuf);
396 /***************************************************************************
398 * CFilterGraph::IFilterGraph2 methods
402 static HRESULT WINAPI
403 IFilterGraph2_fnQueryInterface(IFilterGraph2* iface,REFIID riid,void** ppobj)
405 CFilterGraph_THIS(iface,fgraph);
407 TRACE("(%p)->()\n",This);
409 return IUnknown_QueryInterface(This->unk.punkControl,riid,ppobj);
413 IFilterGraph2_fnAddRef(IFilterGraph2* iface)
415 CFilterGraph_THIS(iface,fgraph);
417 TRACE("(%p)->()\n",This);
419 return IUnknown_AddRef(This->unk.punkControl);
423 IFilterGraph2_fnRelease(IFilterGraph2* iface)
425 CFilterGraph_THIS(iface,fgraph);
427 TRACE("(%p)->()\n",This);
429 return IUnknown_Release(This->unk.punkControl);
432 static HRESULT WINAPI
433 IFilterGraph2_fnAddFilter(IFilterGraph2* iface,IBaseFilter* pFilter, LPCWSTR pName)
435 CFilterGraph_THIS(iface,fgraph);
438 IBaseFilter* pTempFilter;
439 FG_FilterData* pActiveFiltersNew;
441 HRESULT hrSucceeded = S_OK;
444 TRACE( "(%p)->(%p,%s)\n",This,pFilter,debugstr_w(pName) );
446 EnterCriticalSection( &This->m_csFilters );
448 hr = IMediaFilter_GetState(CFilterGraph_IMediaFilter(This),0,&fs);
449 if ( hr == VFW_S_STATE_INTERMEDIATE )
450 hr = VFW_E_STATE_CHANGED;
451 if ( fs != State_Stopped )
452 hr = VFW_E_NOT_STOPPED;
456 TRACE( "(%p) search the specified name.\n",This );
460 hr = IFilterGraph2_FindFilterByName( CFilterGraph_IFilterGraph2(This), pName, &pTempFilter );
463 IBaseFilter_Release(pTempFilter);
464 hrSucceeded = VFW_S_DUPLICATE_NAME;
471 iLen = lstrlenW(pName);
474 memcpy( info.achName, pName, sizeof(WCHAR)*iLen );
475 info.achName[iLen] = 0;
479 ZeroMemory( &info, sizeof(info) );
480 hr = IBaseFilter_QueryFilterInfo( pFilter, &info );
483 if ( info.pGraph != NULL )
485 IFilterGraph_Release(info.pGraph);
486 hr = E_FAIL; /* FIXME */
490 hr = IFilterGraph2_FindFilterByName( CFilterGraph_IFilterGraph2(This), pName, &pTempFilter );
493 pName = info.achName;
498 /* generate modified names for this filter.. */
499 iLen = lstrlenW(info.achName);
502 info.achName[iLen++] = ' ';
504 for ( i = 0; i <= 99; i++ )
506 info.achName[iLen+0] = (i%10) + '0';
507 info.achName[iLen+1] = ((i/10)%10) + '0';
508 info.achName[iLen+2] = 0;
510 hr = IFilterGraph2_FindFilterByName( CFilterGraph_IFilterGraph2(This), info.achName, &pTempFilter );
513 pName = info.achName;
518 hr = ( pName == NULL ) ? E_FAIL : VFW_E_DUPLICATE_NAME;
522 TRACE( "(%p) add this filter - %s.\n",This,debugstr_w(pName) );
524 /* register this filter. */
525 pActiveFiltersNew = (FG_FilterData*)QUARTZ_ReallocMem(
526 This->m_pActiveFilters,
527 sizeof(FG_FilterData) * (This->m_cActiveFilters+1) );
528 if ( pActiveFiltersNew == NULL )
533 This->m_pActiveFilters = pActiveFiltersNew;
534 pActiveFiltersNew = &This->m_pActiveFilters[This->m_cActiveFilters];
536 pActiveFiltersNew->pFilter = NULL;
537 pActiveFiltersNew->pPosition = NULL;
538 pActiveFiltersNew->pSeeking = NULL;
539 pActiveFiltersNew->pwszName = NULL;
540 pActiveFiltersNew->cbName = 0;
542 pActiveFiltersNew->cbName = sizeof(WCHAR)*(lstrlenW(pName)+1);
543 pActiveFiltersNew->pwszName = (WCHAR*)QUARTZ_AllocMem( pActiveFiltersNew->cbName );
544 if ( pActiveFiltersNew->pwszName == NULL )
549 memcpy( pActiveFiltersNew->pwszName, pName, pActiveFiltersNew->cbName );
551 pActiveFiltersNew->pFilter = pFilter;
552 IBaseFilter_AddRef(pFilter);
553 IBaseFilter_QueryInterface( pFilter, &IID_IMediaPosition, (void**)&pActiveFiltersNew->pPosition );
554 IBaseFilter_QueryInterface( pFilter, &IID_IMediaSeeking, (void**)&pActiveFiltersNew->pSeeking );
556 This->m_cActiveFilters ++;
558 hr = IBaseFilter_JoinFilterGraph(pFilter,(IFilterGraph*)iface,pName);
561 EnterCriticalSection( &This->m_csClock );
562 hr = IBaseFilter_SetSyncSource( pFilter, This->m_pClock );
563 LeaveCriticalSection( &This->m_csClock );
567 IBaseFilter_JoinFilterGraph(pFilter,NULL,pName);
568 IFilterGraph2_RemoveFilter(CFilterGraph_IFilterGraph2(This),pFilter);
572 hr = CFilterGraph_GraphChanged(This);
578 LeaveCriticalSection( &This->m_csFilters );
580 TRACE( "(%p) return %08lx\n", This, hr );
585 static HRESULT WINAPI
586 IFilterGraph2_fnRemoveFilter(IFilterGraph2* iface,IBaseFilter* pFilter)
588 CFilterGraph_THIS(iface,fgraph);
591 HRESULT hr = NOERROR;
593 TRACE( "(%p)->(%p)\n",This,pFilter );
595 EnterCriticalSection( &This->m_csFilters );
597 hr = IMediaFilter_GetState(CFilterGraph_IMediaFilter(This),0,&fs);
598 if ( hr == VFW_S_STATE_INTERMEDIATE )
599 hr = VFW_E_STATE_CHANGED;
600 if ( fs != State_Stopped )
601 hr = VFW_E_NOT_STOPPED;
605 hr = S_FALSE; /* FIXME? */
607 for ( n = 0; n < This->m_cActiveFilters; n++ )
609 if ( This->m_pActiveFilters[n].pFilter == pFilter )
611 CFilterGraph_DisconnectAllPins(pFilter);
612 (void)IBaseFilter_SetSyncSource( pFilter, NULL );
613 (void)IBaseFilter_JoinFilterGraph(
614 pFilter, NULL, This->m_pActiveFilters[n].pwszName );
616 if ( This->m_pActiveFilters[n].pFilter != NULL )
617 IMediaPosition_Release(This->m_pActiveFilters[n].pFilter);
618 if ( This->m_pActiveFilters[n].pPosition != NULL )
619 IMediaPosition_Release(This->m_pActiveFilters[n].pPosition);
620 if ( This->m_pActiveFilters[n].pSeeking != NULL )
621 IMediaSeeking_Release(This->m_pActiveFilters[n].pSeeking);
622 if ( This->m_pActiveFilters[n].pwszName != NULL )
623 QUARTZ_FreeMem(This->m_pActiveFilters[n].pwszName);
625 copy = This->m_cActiveFilters - n - 1;
627 memmove( &This->m_pActiveFilters[n],
628 &This->m_pActiveFilters[n+1],
629 sizeof(FG_FilterData) * copy );
630 This->m_cActiveFilters --;
632 hr = CFilterGraph_GraphChanged(This);
638 LeaveCriticalSection( &This->m_csFilters );
643 static HRESULT WINAPI
644 IFilterGraph2_fnEnumFilters(IFilterGraph2* iface,IEnumFilters** ppEnum)
646 CFilterGraph_THIS(iface,fgraph);
647 QUARTZ_CompList* pList = NULL;
651 TRACE( "(%p)->(%p)\n",This,ppEnum );
653 EnterCriticalSection( &This->m_csFilters );
655 pList = QUARTZ_CompList_Alloc();
661 for ( n = 0; n < This->m_cActiveFilters; n++ )
663 hr = QUARTZ_CompList_AddTailComp(
664 pList, (IUnknown*)This->m_pActiveFilters[n].pFilter, NULL, 0 );
669 hr = QUARTZ_CreateEnumUnknown(
670 &IID_IEnumFilters, (void**)ppEnum, pList );
673 QUARTZ_CompList_Free( pList );
675 LeaveCriticalSection( &This->m_csFilters );
680 static HRESULT WINAPI
681 IFilterGraph2_fnFindFilterByName(IFilterGraph2* iface,LPCWSTR pName,IBaseFilter** ppFilter)
683 CFilterGraph_THIS(iface,fgraph);
686 HRESULT hr = E_FAIL; /* FIXME */
688 TRACE( "(%p)->(%s,%p)\n",This,debugstr_w(pName),ppFilter );
690 if ( pName == NULL || ppFilter == NULL )
694 cbName = sizeof(WCHAR) * (lstrlenW(pName) + 1);
696 EnterCriticalSection( &This->m_csFilters );
698 for ( n = 0; n < This->m_cActiveFilters; n++ )
700 if ( This->m_pActiveFilters[n].cbName == cbName &&
701 !memcmp( This->m_pActiveFilters[n].pwszName, pName, cbName ) )
703 *ppFilter = This->m_pActiveFilters[n].pFilter;
704 IBaseFilter_AddRef(*ppFilter);
709 LeaveCriticalSection( &This->m_csFilters );
714 static HRESULT WINAPI
715 IFilterGraph2_fnConnectDirect(IFilterGraph2* iface,IPin* pOut,IPin* pIn,const AM_MEDIA_TYPE* pmt)
717 CFilterGraph_THIS(iface,fgraph);
722 FILTER_INFO finfoOut;
726 TRACE( "(%p)->(%p,%p,%p)\n",This,pOut,pIn,pmt );
728 infoIn.pFilter = NULL;
729 infoOut.pFilter = NULL;
730 finfoIn.pGraph = NULL;
731 finfoOut.pGraph = NULL;
733 EnterCriticalSection( &This->m_csFilters );
735 hr = IMediaFilter_GetState(CFilterGraph_IMediaFilter(This),0,&fs);
736 if ( hr == VFW_S_STATE_INTERMEDIATE )
737 hr = VFW_E_STATE_CHANGED;
738 if ( fs != State_Stopped )
739 hr = VFW_E_NOT_STOPPED;
743 hr = IPin_QueryPinInfo(pIn,&infoIn);
746 hr = IPin_QueryPinInfo(pOut,&infoOut);
749 if ( infoIn.pFilter == NULL || infoOut.pFilter == NULL ||
750 infoIn.dir != PINDIR_INPUT || infoOut.dir != PINDIR_OUTPUT )
756 hr = IBaseFilter_QueryFilterInfo(infoIn.pFilter,&finfoIn);
759 hr = IBaseFilter_QueryFilterInfo(infoOut.pFilter,&finfoOut);
762 if ( finfoIn.pGraph != ((IFilterGraph*)iface) ||
763 finfoOut.pGraph != ((IFilterGraph*)iface) )
770 hr = IPin_ConnectedTo(pIn,&pConnTo);
771 if ( hr == NOERROR && pConnTo != NULL )
773 IPin_Release(pConnTo);
774 hr = VFW_E_ALREADY_CONNECTED;
779 hr = IPin_ConnectedTo(pOut,&pConnTo);
780 if ( hr == NOERROR && pConnTo != NULL )
782 IPin_Release(pConnTo);
783 hr = VFW_E_ALREADY_CONNECTED;
787 TRACE("(%p) try to connect %p<->%p\n",This,pIn,pOut);
788 hr = IPin_Connect(pOut,pIn,pmt);
791 TRACE("(%p)->Connect(%p,%p) hr = %08lx\n",pOut,pIn,pmt,hr);
792 IPin_Disconnect(pOut);
793 IPin_Disconnect(pIn);
797 hr = CFilterGraph_GraphChanged(This);
802 LeaveCriticalSection( &This->m_csFilters );
804 if ( infoIn.pFilter != NULL )
805 IBaseFilter_Release(infoIn.pFilter);
806 if ( infoOut.pFilter != NULL )
807 IBaseFilter_Release(infoOut.pFilter);
808 if ( finfoIn.pGraph != NULL )
809 IFilterGraph_Release(finfoIn.pGraph);
810 if ( finfoOut.pGraph != NULL )
811 IFilterGraph_Release(finfoOut.pGraph);
816 static HRESULT WINAPI
817 IFilterGraph2_fnReconnect(IFilterGraph2* iface,IPin* pPin)
819 CFilterGraph_THIS(iface,fgraph);
821 TRACE( "(%p)->(%p)\n",This,pPin );
823 return IFilterGraph2_ReconnectEx(iface,pPin,NULL);
826 static HRESULT WINAPI
827 IFilterGraph2_fnDisconnect(IFilterGraph2* iface,IPin* pPin)
829 CFilterGraph_THIS(iface,fgraph);
833 TRACE( "(%p)->(%p)\n",This,pPin );
835 EnterCriticalSection( &This->m_csFilters );
838 hr = IPin_ConnectedTo(pPin,&pConnTo);
839 if ( hr == NOERROR && pConnTo != NULL )
841 IPin_Disconnect(pConnTo);
842 IPin_Release(pConnTo);
844 hr = IPin_Disconnect(pPin);
848 hr = CFilterGraph_GraphChanged(This);
853 LeaveCriticalSection( &This->m_csFilters );
858 static HRESULT WINAPI
859 IFilterGraph2_fnSetDefaultSyncSource(IFilterGraph2* iface)
861 CFilterGraph_THIS(iface,fgraph);
863 IReferenceClock* pClock;
866 FIXME( "(%p)->() stub!\n", This );
868 /* FIXME - search all filters from renderer. */
870 hr = QUARTZ_CreateSystemClock( NULL, (void**)&punk );
873 hr = IUnknown_QueryInterface( punk, &IID_IReferenceClock, (void**)&pClock ); IUnknown_Release( punk );
877 hr = IMediaFilter_SetSyncSource(
878 CFilterGraph_IMediaFilter(This), pClock );
879 IReferenceClock_Release( pClock );
884 static HRESULT WINAPI
885 IFilterGraph2_fnConnect(IFilterGraph2* iface,IPin* pOut,IPin* pIn)
887 CFilterGraph_THIS(iface,fgraph);
888 IFilterMapper2* pMap2 = NULL;
889 IEnumMoniker* pEnumMon = NULL;
890 IMoniker* pMon = NULL;
891 IBaseFilter* pFilter = NULL;
892 IEnumPins* pEnumPin = NULL;
893 IPin* pPinTry = NULL;
895 PIN_DIRECTION pindir;
898 BOOL bConnected = FALSE;
899 CLSID clsidOutFilter;
904 TRACE( "(%p)->(%p,%p)\n",This,pOut,pIn );
906 /* At first, try to connect directly. */
907 hr = IFilterGraph_ConnectDirect(iface,pOut,pIn,NULL);
911 /* FIXME - try to connect indirectly. */
912 FIXME( "(%p)->(%p,%p) stub!\n",This,pOut,pIn );
915 hr = IPin_QueryPinInfo(pOut,&info);
918 if ( info.pFilter == NULL )
920 hr = IBaseFilter_GetClassID(info.pFilter,&clsidOutFilter);
921 IBaseFilter_Release(info.pFilter);
926 hr = IPin_QueryPinInfo(pIn,&info);
929 if ( info.pFilter == NULL )
931 hr = IBaseFilter_GetClassID(info.pFilter,&clsidInFilter);
932 IBaseFilter_Release(info.pFilter);
936 /* FIXME - try to connect with unused filters. */
937 /* FIXME - try to connect with cached filters. */
938 /* FIXME - enumerate transform filters and try to connect */
939 hr = CoCreateInstance(
940 &CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
941 &IID_IFilterMapper2, (void**)&pMap2 );
944 hr = IFilterMapper2_EnumMatchingFilters(
945 pMap2,&pEnumMon,0,FALSE,MERIT_DO_NOT_USE+1,
946 TRUE,0,NULL,NULL,NULL,FALSE,
947 TRUE,0,NULL,NULL,NULL);
948 IFilterMapper2_Release(pMap2);
951 TRACE("try to connect indirectly\n");
955 while ( !bConnected && hr == S_OK )
957 hr = IEnumMoniker_Next(pEnumMon,1,&pMon,&cReturned);
960 hr = IMoniker_BindToObject(pMon,NULL,NULL,&IID_IBaseFilter,(void**)&pFilter );
963 hr = IBaseFilter_GetClassID(pFilter,&clsid);
965 ( IsEqualGUID(&clsidOutFilter,&clsid) || IsEqualGUID(&clsidInFilter,&clsid) ) )
968 hr = IFilterGraph2_AddFilter(iface,pFilter,NULL);
972 hr = IBaseFilter_EnumPins(pFilter,&pEnumPin);
976 while ( !bTryConnect )
978 hr = IEnumPins_Next(pEnumPin,1,&pPinTry,&cReturned);
981 hr = IPin_QueryDirection(pPinTry,&pindir);
982 if ( hr == S_OK && pindir == PINDIR_INPUT )
984 /* try to connect directly. */
985 hr = IFilterGraph2_ConnectDirect(iface,pOut,pPinTry,NULL);
990 IPin_Release(pPinTry); pPinTry = NULL;
993 IEnumPins_Release(pEnumPin); pEnumPin = NULL;
995 TRACE("TryConnect %d\n",bTryConnect);
999 hr = IBaseFilter_EnumPins(pFilter,&pEnumPin);
1002 while ( !bConnected )
1004 hr = IEnumPins_Next(pEnumPin,1,&pPinTry,&cReturned);
1007 hr = IPin_QueryDirection(pPinTry,&pindir);
1008 if ( hr == S_OK && pindir == PINDIR_OUTPUT )
1010 /* try to connect indirectly. */
1011 hr = IFilterGraph2_Connect(iface,pPinTry,pIn);
1016 IPin_Release(pPinTry); pPinTry = NULL;
1018 IEnumPins_Release(pEnumPin); pEnumPin = NULL;
1022 hr = IFilterGraph2_RemoveFilter(iface,pFilter);
1024 IBaseFilter_Release(pFilter); pFilter = NULL;
1025 if ( SUCCEEDED(hr) )
1032 IMoniker_Release(pMon); pMon = NULL;
1034 IEnumMoniker_Release(pEnumMon); pEnumMon = NULL;
1037 if ( SUCCEEDED(hr) && !bConnected )
1038 hr = VFW_E_CANNOT_CONNECT;
1043 static HRESULT WINAPI
1044 IFilterGraph2_fnRender(IFilterGraph2* iface,IPin* pOut)
1046 CFilterGraph_THIS(iface,fgraph);
1048 TRACE( "(%p)->(%p)\n",This,pOut);
1050 return IFilterGraph2_RenderEx( CFilterGraph_IFilterGraph2(This), pOut, 0, NULL );
1053 static HRESULT WINAPI
1054 IFilterGraph2_fnRenderFile(IFilterGraph2* iface,LPCWSTR lpFileName,LPCWSTR lpPlayList)
1056 CFilterGraph_THIS(iface,fgraph);
1058 IBaseFilter* pFilter = NULL;
1059 IEnumPins* pEnum = NULL;
1066 TRACE( "(%p)->(%s,%s)\n",This,
1067 debugstr_w(lpFileName),debugstr_w(lpPlayList) );
1069 if ( lpPlayList != NULL )
1070 return E_INVALIDARG;
1073 hr = IFilterGraph2_AddSourceFilter(iface,lpFileName,NULL,&pFilter);
1076 if ( pFilter == NULL )
1081 TRACE("(%p) source filter %p\n",This,pFilter);
1084 hr = IBaseFilter_EnumPins( pFilter, &pEnum );
1087 if ( pEnum == NULL )
1100 hr = IEnumPins_Next( pEnum, 1, &pPin, &cFetched );
1103 if ( hr != NOERROR || pPin == NULL || cFetched != 1 )
1108 hr = IPin_QueryDirection( pPin, &dir );
1109 if ( hr == NOERROR && dir == PINDIR_OUTPUT )
1112 hr = IFilterGraph2_Render( iface, pPin );
1113 if ( hr == NOERROR )
1116 IPin_Release( pPin );
1119 if ( hr == NOERROR )
1121 if ( cTryToRender > cActRender )
1122 hr = VFW_S_PARTIAL_RENDER;
1123 if ( cActRender == 0 )
1128 if ( pEnum != NULL )
1129 IEnumPins_Release( pEnum );
1130 if ( pFilter != NULL )
1131 IBaseFilter_Release( pFilter );
1136 static HRESULT WINAPI
1137 IFilterGraph2_fnAddSourceFilter(IFilterGraph2* iface,LPCWSTR lpFileName,LPCWSTR lpFilterName,IBaseFilter** ppBaseFilter)
1139 CFilterGraph_THIS(iface,fgraph);
1141 BYTE bStartData[512];
1145 IFileSourceFilter* pSource;
1147 FIXME( "(%p)->(%s,%s,%p)\n",This,
1148 debugstr_w(lpFileName),debugstr_w(lpFilterName),ppBaseFilter );
1150 if ( lpFileName == NULL || ppBaseFilter == NULL )
1152 *ppBaseFilter = NULL;
1154 hr = QUARTZ_PeekFile( lpFileName, bStartData, 512, &cbStartData );
1157 FIXME("cannot open %s (NOTE: URL is not implemented)\n", debugstr_w(lpFileName));
1160 ZeroMemory( &mt, sizeof(AM_MEDIA_TYPE) );
1161 mt.bFixedSizeSamples = 1;
1163 memcpy( &mt.majortype, &MEDIATYPE_Stream, sizeof(GUID) );
1164 memcpy( &mt.subtype, &MEDIASUBTYPE_NULL, sizeof(GUID) );
1165 memcpy( &mt.formattype, &FORMAT_None, sizeof(GUID) );
1166 hr = QUARTZ_GetSourceTypeFromData(
1167 bStartData, cbStartData,
1168 &mt.majortype, &mt.subtype, &clsidSource );
1171 ERR("QUARTZ_GetSourceTypeFromData() failed - return %08lx\n",hr);
1176 FIXME( "file %s - unknown format\n", debugstr_w(lpFileName) );
1177 return VFW_E_INVALID_FILE_FORMAT;
1180 hr = CoCreateInstance(
1181 &clsidSource, NULL, CLSCTX_INPROC_SERVER,
1182 &IID_IBaseFilter, (void**)ppBaseFilter );
1185 hr = IBaseFilter_QueryInterface(*ppBaseFilter,&IID_IFileSourceFilter,(void**)&pSource);
1186 if ( SUCCEEDED(hr) )
1188 hr = IFileSourceFilter_Load(pSource,lpFileName,&mt);
1189 IFileSourceFilter_Release(pSource);
1191 if ( SUCCEEDED(hr) )
1192 hr = IFilterGraph2_AddFilter(iface,*ppBaseFilter,lpFilterName);
1195 IBaseFilter_Release(*ppBaseFilter);
1196 *ppBaseFilter = NULL;
1203 static HRESULT WINAPI
1204 IFilterGraph2_fnSetLogFile(IFilterGraph2* iface,DWORD_PTR hFile)
1206 CFilterGraph_THIS(iface,fgraph);
1208 FIXME( "(%p)->() stub!\n", This );
1210 return S_OK; /* no debug output */
1213 static HRESULT WINAPI
1214 IFilterGraph2_fnAbort(IFilterGraph2* iface)
1216 CFilterGraph_THIS(iface,fgraph);
1218 FIXME( "(%p)->() stub!\n", This );
1220 /* FIXME - abort the current asynchronous task. */
1225 static HRESULT WINAPI
1226 IFilterGraph2_fnShouldOperationContinue(IFilterGraph2* iface)
1228 CFilterGraph_THIS(iface,fgraph);
1230 FIXME( "(%p)->() stub!\n", This );
1235 static HRESULT WINAPI
1236 IFilterGraph2_fnAddSourceFilterForMoniker(IFilterGraph2* iface,IMoniker* pMon,IBindCtx* pCtx,LPCWSTR pFilterName,IBaseFilter** ppFilter)
1238 CFilterGraph_THIS(iface,fgraph);
1240 FIXME( "(%p)->() stub!\n", This );
1244 static HRESULT WINAPI
1245 IFilterGraph2_fnReconnectEx(IFilterGraph2* iface,IPin* pPin,const AM_MEDIA_TYPE* pmt)
1247 CFilterGraph_THIS(iface,fgraph);
1250 FIXME( "(%p)->(%p,%p) stub!\n",This,pPin,pmt );
1252 /* reconnect asynchronously. */
1254 EnterCriticalSection( &This->m_csFilters );
1255 hr = CFilterGraph_GraphChanged(This);
1256 LeaveCriticalSection( &This->m_csFilters );
1261 static HRESULT WINAPI
1262 IFilterGraph2_fnRenderEx(IFilterGraph2* iface,IPin* pOut,DWORD dwFlags,DWORD* pdwReserved)
1264 CFilterGraph_THIS(iface,fgraph);
1266 IFilterMapper2* pMap2 = NULL;
1267 IEnumMoniker* pEnumMon = NULL;
1268 IMoniker* pMon = NULL;
1269 IBaseFilter* pFilter = NULL;
1270 IEnumPins* pEnumPin = NULL;
1272 PIN_DIRECTION pindir;
1273 BOOL bRendered = FALSE;
1276 FIXME( "(%p)->(%p,%08lx,%p) stub!\n",This,pPin,dwFlags,pdwReserved);
1278 if ( pdwReserved != NULL )
1279 return E_INVALIDARG;
1283 FIXME( "dwFlags != 0...\n" );
1284 return E_INVALIDARG;
1287 /* FIXME - must be locked */
1288 /*EnterCriticalSection( &This->m_csFilters );*/
1293 hr = CoCreateInstance(
1294 &CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
1295 &IID_IFilterMapper2, (void**)&pMap2 );
1298 hr = IFilterMapper2_EnumMatchingFilters(
1299 pMap2,&pEnumMon,0,FALSE,MERIT_DO_NOT_USE+1,
1300 TRUE,0,NULL,NULL,NULL,TRUE,
1301 FALSE,0,NULL,NULL,NULL);
1302 IFilterMapper2_Release(pMap2);
1305 TRACE("try to render pin\n");
1309 /* try to render pin. */
1310 while ( !bRendered && hr == S_OK )
1312 hr = IEnumMoniker_Next(pEnumMon,1,&pMon,&cReturned);
1315 hr = IMoniker_BindToObject(pMon,NULL,NULL,&IID_IBaseFilter,(void**)&pFilter );
1318 hr = IFilterGraph2_AddFilter(iface,pFilter,NULL);
1321 hr = IBaseFilter_EnumPins(pFilter,&pEnumPin);
1324 while ( !bRendered )
1326 hr = IEnumPins_Next(pEnumPin,1,&pPin,&cReturned);
1329 hr = IPin_QueryDirection(pPin,&pindir);
1330 if ( hr == S_OK && pindir == PINDIR_INPUT )
1332 /* try to connect. */
1333 hr = IFilterGraph2_Connect(iface,pOut,pPin);
1338 IPin_Release(pPin); pPin = NULL;
1340 IEnumPins_Release(pEnumPin); pEnumPin = NULL;
1343 hr = IFilterGraph2_RemoveFilter(iface,pFilter);
1345 IBaseFilter_Release(pFilter); pFilter = NULL;
1346 if ( SUCCEEDED(hr) )
1353 IMoniker_Release(pMon); pMon = NULL;
1355 IEnumMoniker_Release(pEnumMon); pEnumMon = NULL;
1360 /* successfully rendered(but may be partial now) */
1363 /* FIXME - try to render all inserted filters. */
1364 /* hr = VFW_S_PARTIAL_RENDER; */
1368 if ( SUCCEEDED(hr) )
1369 hr = VFW_E_CANNOT_RENDER;
1372 /*LeaveCriticalSection( &This->m_csFilters );*/
1380 static ICOM_VTABLE(IFilterGraph2) ifgraph =
1382 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1383 /* IUnknown fields */
1384 IFilterGraph2_fnQueryInterface,
1385 IFilterGraph2_fnAddRef,
1386 IFilterGraph2_fnRelease,
1387 /* IFilterGraph fields */
1388 IFilterGraph2_fnAddFilter,
1389 IFilterGraph2_fnRemoveFilter,
1390 IFilterGraph2_fnEnumFilters,
1391 IFilterGraph2_fnFindFilterByName,
1392 IFilterGraph2_fnConnectDirect,
1393 IFilterGraph2_fnReconnect,
1394 IFilterGraph2_fnDisconnect,
1395 IFilterGraph2_fnSetDefaultSyncSource,
1396 /* IGraphBuilder fields */
1397 IFilterGraph2_fnConnect,
1398 IFilterGraph2_fnRender,
1399 IFilterGraph2_fnRenderFile,
1400 IFilterGraph2_fnAddSourceFilter,
1401 IFilterGraph2_fnSetLogFile,
1402 IFilterGraph2_fnAbort,
1403 IFilterGraph2_fnShouldOperationContinue,
1404 /* IFilterGraph2 fields */
1405 IFilterGraph2_fnAddSourceFilterForMoniker,
1406 IFilterGraph2_fnReconnectEx,
1407 IFilterGraph2_fnRenderEx,
1410 HRESULT CFilterGraph_InitIFilterGraph2( CFilterGraph* pfg )
1412 TRACE("(%p)\n",pfg);
1413 ICOM_VTBL(&pfg->fgraph) = &ifgraph;
1415 InitializeCriticalSection( &pfg->m_csFilters );
1416 pfg->m_cActiveFilters = 0;
1417 pfg->m_pActiveFilters = NULL;
1422 void CFilterGraph_UninitIFilterGraph2( CFilterGraph* pfg )
1424 TRACE("(%p)\n",pfg);
1426 /* remove all filters... */
1427 while ( pfg->m_cActiveFilters > 0 )
1429 IFilterGraph2_RemoveFilter(
1430 CFilterGraph_IFilterGraph2(pfg),
1431 pfg->m_pActiveFilters[pfg->m_cActiveFilters-1].pFilter );
1434 if ( pfg->m_pActiveFilters != NULL )
1435 QUARTZ_FreeMem( pfg->m_pActiveFilters );
1437 DeleteCriticalSection( &pfg->m_csFilters );
1440 /***************************************************************************
1442 * CFilterGraph::IGraphVersion methods
1446 static HRESULT WINAPI
1447 IGraphVersion_fnQueryInterface(IGraphVersion* iface,REFIID riid,void** ppobj)
1449 CFilterGraph_THIS(iface,graphversion);
1451 TRACE("(%p)->()\n",This);
1453 return IUnknown_QueryInterface(This->unk.punkControl,riid,ppobj);
1457 IGraphVersion_fnAddRef(IGraphVersion* iface)
1459 CFilterGraph_THIS(iface,graphversion);
1461 TRACE("(%p)->()\n",This);
1463 return IUnknown_AddRef(This->unk.punkControl);
1467 IGraphVersion_fnRelease(IGraphVersion* iface)
1469 CFilterGraph_THIS(iface,graphversion);
1471 TRACE("(%p)->()\n",This);
1473 return IUnknown_Release(This->unk.punkControl);
1477 static HRESULT WINAPI
1478 IGraphVersion_fnQueryVersion(IGraphVersion* iface,LONG* plVersion)
1480 CFilterGraph_THIS(iface,graphversion);
1482 TRACE("(%p)->(%p)\n",This,plVersion);
1484 if ( plVersion == NULL )
1487 EnterCriticalSection( &This->m_csFilters );
1488 *plVersion = This->m_lGraphVersion;
1489 LeaveCriticalSection( &This->m_csFilters );
1495 static ICOM_VTABLE(IGraphVersion) igraphversion =
1497 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1498 /* IUnknown fields */
1499 IGraphVersion_fnQueryInterface,
1500 IGraphVersion_fnAddRef,
1501 IGraphVersion_fnRelease,
1502 /* IGraphVersion fields */
1503 IGraphVersion_fnQueryVersion,
1508 HRESULT CFilterGraph_InitIGraphVersion( CFilterGraph* pfg )
1510 TRACE("(%p)\n",pfg);
1511 ICOM_VTBL(&pfg->graphversion) = &igraphversion;
1513 pfg->m_lGraphVersion = 1;
1518 void CFilterGraph_UninitIGraphVersion( CFilterGraph* pfg )
1520 TRACE("(%p)\n",pfg);
1523 /***************************************************************************
1525 * CFilterGraph::IGraphConfig methods
1529 static HRESULT WINAPI
1530 IGraphConfig_fnQueryInterface(IGraphConfig* iface,REFIID riid,void** ppobj)
1532 CFilterGraph_THIS(iface,grphconf);
1534 TRACE("(%p)->()\n",This);
1536 return IUnknown_QueryInterface(This->unk.punkControl,riid,ppobj);
1540 IGraphConfig_fnAddRef(IGraphConfig* iface)
1542 CFilterGraph_THIS(iface,grphconf);
1544 TRACE("(%p)->()\n",This);
1546 return IUnknown_AddRef(This->unk.punkControl);
1550 IGraphConfig_fnRelease(IGraphConfig* iface)
1552 CFilterGraph_THIS(iface,grphconf);
1554 TRACE("(%p)->()\n",This);
1556 return IUnknown_Release(This->unk.punkControl);
1561 static HRESULT WINAPI
1562 IGraphConfig_fnReconnect(IGraphConfig* iface,IPin* pOut,IPin* pIn,const AM_MEDIA_TYPE* pmt,IBaseFilter* pFilter,HANDLE hAbort,DWORD dwFlags)
1564 CFilterGraph_THIS(iface,grphconf);
1566 FIXME("(%p)->() stub!\n",This);
1571 static HRESULT WINAPI
1572 IGraphConfig_fnReconfigure(IGraphConfig* iface,IGraphConfigCallback* pCallback,PVOID pvParam,DWORD dwFlags,HANDLE hAbort)
1574 CFilterGraph_THIS(iface,grphconf);
1577 FIXME("(%p)->(%p,%p,%08lx,%08x) stub!\n",This,pCallback,pvParam,dwFlags,hAbort);
1579 EnterCriticalSection( &This->m_csFilters );
1580 EnterCriticalSection( &This->m_csGraphState );
1582 hr = IGraphConfigCallback_Reconfigure(pCallback,pvParam,dwFlags);
1584 LeaveCriticalSection( &This->m_csGraphState );
1585 LeaveCriticalSection( &This->m_csFilters );
1590 static HRESULT WINAPI
1591 IGraphConfig_fnAddFilterToCache(IGraphConfig* iface,IBaseFilter* pFilter)
1593 CFilterGraph_THIS(iface,grphconf);
1595 FIXME("(%p)->() stub!\n",This);
1600 static HRESULT WINAPI
1601 IGraphConfig_fnEnumCacheFilter(IGraphConfig* iface,IEnumFilters** ppenum)
1603 CFilterGraph_THIS(iface,grphconf);
1605 FIXME("(%p)->() stub!\n",This);
1610 static HRESULT WINAPI
1611 IGraphConfig_fnRemoveFilterFromCache(IGraphConfig* iface,IBaseFilter* pFilter)
1613 CFilterGraph_THIS(iface,grphconf);
1615 FIXME("(%p)->() stub!\n",This);
1620 static HRESULT WINAPI
1621 IGraphConfig_fnGetStartTime(IGraphConfig* iface,REFERENCE_TIME* prt)
1623 CFilterGraph_THIS(iface,grphconf);
1625 FIXME("(%p)->() stub!\n",This);
1630 static HRESULT WINAPI
1631 IGraphConfig_fnPushThroughData(IGraphConfig* iface,IPin* pOut,IPinConnection* pConn,HANDLE hAbort)
1633 CFilterGraph_THIS(iface,grphconf);
1635 FIXME("(%p)->() stub!\n",This);
1640 static HRESULT WINAPI
1641 IGraphConfig_fnSetFilterFlags(IGraphConfig* iface,IBaseFilter* pFilter,DWORD dwFlags)
1643 CFilterGraph_THIS(iface,grphconf);
1645 FIXME("(%p)->() stub!\n",This);
1650 static HRESULT WINAPI
1651 IGraphConfig_fnGetFilterFlags(IGraphConfig* iface,IBaseFilter* pFilter,DWORD* pdwFlags)
1653 CFilterGraph_THIS(iface,grphconf);
1655 FIXME("(%p)->() stub!\n",This);
1660 static HRESULT WINAPI
1661 IGraphConfig_fnRemoveFilterEx(IGraphConfig* iface,IBaseFilter* pFilter,DWORD dwFlags)
1663 CFilterGraph_THIS(iface,grphconf);
1665 FIXME("(%p)->() stub!\n",This);
1674 static ICOM_VTABLE(IGraphConfig) igraphconfig =
1676 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1677 /* IUnknown fields */
1678 IGraphConfig_fnQueryInterface,
1679 IGraphConfig_fnAddRef,
1680 IGraphConfig_fnRelease,
1681 /* IGraphConfig fields */
1682 IGraphConfig_fnReconnect,
1683 IGraphConfig_fnReconfigure,
1684 IGraphConfig_fnAddFilterToCache,
1685 IGraphConfig_fnEnumCacheFilter,
1686 IGraphConfig_fnRemoveFilterFromCache,
1687 IGraphConfig_fnGetStartTime,
1688 IGraphConfig_fnPushThroughData,
1689 IGraphConfig_fnSetFilterFlags,
1690 IGraphConfig_fnGetFilterFlags,
1691 IGraphConfig_fnRemoveFilterEx,
1696 HRESULT CFilterGraph_InitIGraphConfig( CFilterGraph* pfg )
1698 TRACE("(%p)\n",pfg);
1699 ICOM_VTBL(&pfg->grphconf) = &igraphconfig;
1704 void CFilterGraph_UninitIGraphConfig( CFilterGraph* pfg )
1706 TRACE("(%p)\n",pfg);