168
|
1 #include <stdio.h>
|
|
2 #include <string.h>
|
|
3 #include "DS_Filter.h"
|
|
4 #include <except.h>
|
|
5 //#include "../loader/loader.h"
|
|
6 #include <string>
|
|
7 #define __MODULE__ "DirectShow generic filter"
|
|
8
|
|
9 using namespace std;
|
|
10
|
|
11 typedef long STDCALL (*GETCLASS) (const GUID*, const GUID*, void**);
|
|
12 extern "C" char* def_path;
|
|
13
|
|
14 extern "C" int STDCALL LoadLibraryA(const char*);
|
|
15 extern "C" STDCALL void* GetProcAddress(int, const char*);
|
|
16 extern "C" int STDCALL FreeLibrary(int);
|
|
17
|
244
|
18 extern "C" void Setup_LDT_Keeper();
|
|
19 extern "C" void setup_FS_Segment();
|
|
20
|
168
|
21 DS_Filter::DS_Filter()
|
|
22 :m_iHandle(0), m_pFilter(0), m_pInputPin(0),
|
|
23 m_pOutputPin(0), m_pSrcFilter(0),
|
|
24 m_pOurInput(0), m_pOurOutput(0),
|
|
25 m_pImp(0), m_pAll(0), m_pParentFilter(0)
|
|
26 {
|
|
27 }
|
|
28
|
|
29 void DS_Filter::Create(string dllname, const GUID* id, AM_MEDIA_TYPE* in_fmt, AM_MEDIA_TYPE* out_fmt)
|
|
30 {
|
244
|
31
|
|
32 Setup_LDT_Keeper();
|
|
33
|
168
|
34 try
|
|
35 {
|
|
36 string _fullname=def_path;
|
|
37 _fullname+="/";
|
|
38 _fullname+=dllname;
|
|
39 m_iHandle= LoadLibraryA(_fullname.c_str());
|
|
40 if(!m_iHandle)throw FATAL("Could not open DLL");
|
|
41 GETCLASS func=(GETCLASS)GetProcAddress(m_iHandle, "DllGetClassObject");
|
|
42 if(!func)throw FATAL("Illegal or corrupt DLL");
|
|
43
|
|
44 HRESULT result;
|
|
45 IClassFactory* factory=0;
|
|
46 IUnknown* object=0;
|
|
47
|
|
48 result=func(id, &IID_IClassFactory, (void**)&factory);
|
|
49 if(result || (!factory)) throw FATAL("No such class object");;
|
|
50
|
244
|
51 // printf("# factory = %X\n",(unsigned int)factory);
|
|
52 // printf("# factory->vt = %X\n",(unsigned int)factory->vt);
|
|
53 // printf("# factory->vt->CreateInstance = %X\n",(unsigned int)factory->vt->CreateInstance);
|
|
54
|
|
55 setup_FS_Segment();
|
168
|
56
|
|
57 printf("Calling factory->vt->CreateInstance()\n");
|
|
58 result=factory->vt->CreateInstance(factory, 0, &IID_IUnknown, (void**)&object);
|
|
59 printf("Calling factory->vt->Release()\n");
|
|
60
|
|
61 // result=factory->vt->CreateInstance(factory, 0, &IID_IUnknown, (void**)&object);
|
|
62
|
|
63 printf("CreateInstance ok %x\n",result);
|
|
64
|
|
65 factory->vt->Release((IUnknown*)factory);
|
|
66 if(result || (!object)) throw FATAL("Class factory failure");
|
|
67
|
|
68 result=object->vt->QueryInterface(object, &IID_IBaseFilter, (void**)&m_pFilter);
|
|
69 object->vt->Release((IUnknown*)object);
|
|
70 if(result || (!m_pFilter)) throw FATAL("Object does not have IBaseFilter interface");
|
|
71
|
|
72 IEnumPins* enum_pins=0;
|
|
73 // enumerate pins
|
|
74 result=m_pFilter->vt->EnumPins(m_pFilter, &enum_pins);
|
|
75 if(result || (!enum_pins)) throw FATAL("Could not enumerate pins");
|
|
76 IPin* array[256];
|
|
77 ULONG fetched;
|
|
78 enum_pins->vt->Reset(enum_pins);
|
|
79 result=enum_pins->vt->Next(enum_pins, (ULONG)256, (IPin**)array, &fetched);
|
|
80 printf("Pins enumeration returned %d pins, error is %x\n", fetched, result);
|
|
81
|
|
82 for(int i=0; i<fetched; i++)
|
|
83 {
|
|
84 int direction=-1;
|
|
85 array[i]->vt->QueryDirection(array[i], (PIN_DIRECTION*)&direction);
|
|
86 if((!m_pInputPin)&&(direction==0))
|
|
87 {
|
|
88 m_pInputPin=array[i];
|
|
89 m_pInputPin->vt->AddRef((IUnknown*)m_pInputPin);
|
|
90 }
|
|
91 if((!m_pOutputPin)&&(direction==1))
|
|
92 {
|
|
93 m_pOutputPin=array[i];
|
|
94 m_pOutputPin->vt->AddRef((IUnknown*)m_pOutputPin);
|
|
95 }
|
|
96 array[i]->vt->Release((IUnknown*)(array[i]));
|
|
97 }
|
|
98 if(!m_pInputPin)throw FATAL("Input pin not found");
|
|
99 if(!m_pOutputPin)throw FATAL("Output pin not found");
|
|
100
|
|
101 result=m_pInputPin->vt->QueryInterface((IUnknown*)m_pInputPin, &IID_IMemInputPin, (void**)&m_pImp);
|
|
102 if(result)
|
|
103 throw FATAL("Error getting IMemInputPin interface");
|
|
104 m_pOurType=in_fmt;
|
|
105 m_pDestType=out_fmt;
|
|
106 result=m_pInputPin->vt->QueryAccept(m_pInputPin, m_pOurType);
|
|
107 if(result) throw FATAL("Source format is not accepted");
|
|
108
|
|
109 m_pParentFilter=new CBaseFilter2;
|
|
110 m_pSrcFilter=new CBaseFilter(*m_pOurType, m_pParentFilter);
|
|
111 m_pOurInput=m_pSrcFilter->GetPin();
|
|
112 m_pOurInput->vt->AddRef((IUnknown*)m_pOurInput);
|
|
113
|
|
114 result=m_pInputPin->vt->ReceiveConnection(m_pInputPin, m_pOurInput, m_pOurType);
|
|
115 if(result) throw FATAL("Error connecting to input pin");
|
|
116
|
|
117 m_pOurOutput=new COutputPin(*m_pDestType);
|
|
118 result=m_pOutputPin->vt->ReceiveConnection(m_pOutputPin,
|
|
119 m_pOurOutput, m_pDestType);
|
|
120 if(result)throw FATAL("Error connecting to output pin");
|
|
121 m_iState=1;
|
|
122 }
|
|
123 catch(FatalError e)
|
|
124 {
|
|
125 e.PrintAll();
|
|
126 if(m_pFilter)m_pFilter->vt->Release((IUnknown*)m_pFilter);
|
|
127 if(m_pOutputPin)m_pOutputPin->vt->Release((IUnknown*)m_pOutputPin);
|
|
128 if(m_pInputPin)m_pInputPin->vt->Release((IUnknown*)m_pInputPin);
|
|
129 if(m_pImp)m_pImp->vt->Release((IUnknown*)m_pImp);
|
|
130 if(m_pOurInput)m_pOurInput->vt->Release((IUnknown*)m_pOurInput);
|
|
131 delete m_pSrcFilter;
|
|
132 delete m_pParentFilter;
|
|
133 delete m_pOurOutput;
|
|
134 if(m_iHandle)FreeLibrary(m_iHandle);
|
|
135 throw;
|
|
136 }
|
|
137 }
|
|
138 void DS_Filter::Start()
|
|
139 {
|
|
140 if(m_iState!=1)
|
|
141 return;
|
|
142
|
|
143 HRESULT hr=m_pFilter->vt->Run(m_pFilter, 0);
|
|
144 if(hr!=0)
|
|
145 {
|
|
146 cerr<<"WARNING: m_Filter->Run() failed, error code "<<hex<<hr<<dec<<endl;
|
|
147 }
|
|
148 hr=m_pImp->vt->GetAllocator(m_pImp, &m_pAll);
|
|
149 if(hr)
|
|
150 {
|
|
151 cerr<<"Error getting IMemAllocator interface "<<hex<<hr<<dec<<endl;
|
|
152 m_pImp->vt->Release((IUnknown*)m_pImp);
|
|
153 return;
|
|
154 }
|
|
155 m_pImp->vt->NotifyAllocator(m_pImp, m_pAll, 0);
|
|
156 m_iState=2;
|
|
157 return;
|
|
158 }
|
|
159 void DS_Filter::Stop()
|
|
160 {
|
|
161 if(m_iState!=2)
|
|
162 return;
|
|
163 m_pAll->vt->Release((IUnknown*)m_pAll);
|
|
164 m_pAll=0;
|
|
165 m_pFilter->vt->Stop(m_pFilter);
|
|
166 m_iState=1;
|
|
167 return;
|
|
168 }
|
|
169 DS_Filter::~DS_Filter()
|
|
170 {
|
|
171 if(m_iState==0)
|
|
172 return;
|
|
173 if(m_iState==2)Stop();
|
|
174 if(m_pInputPin)m_pInputPin->vt->Disconnect(m_pInputPin);
|
|
175 if(m_pOutputPin)m_pOutputPin->vt->Disconnect(m_pOutputPin);
|
|
176 if(m_pFilter)m_pFilter->vt->Release((IUnknown*)m_pFilter);
|
|
177 if(m_pOutputPin)m_pOutputPin->vt->Release((IUnknown*)m_pOutputPin);
|
|
178 if(m_pInputPin)m_pInputPin->vt->Release((IUnknown*)m_pInputPin);
|
|
179 if(m_pOurInput)m_pOurInput->vt->Release((IUnknown*)m_pOurInput);
|
|
180 if(m_pImp)m_pImp->vt->Release((IUnknown*)m_pImp);
|
|
181 delete m_pSrcFilter;
|
|
182 delete m_pParentFilter;
|
|
183 delete m_pOurOutput;
|
|
184 if(m_iHandle)FreeLibrary(m_iHandle);
|
|
185 }
|