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