-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.cpp
253 lines (228 loc) · 6.2 KB
/
runner.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// SPDX-License-Identifier: LGPL-2.0-or-later
#define INITGUID
#define STRSAFE_NO_DEPRECATE
#include <windows.h>
#include <stdio.h>
#include <dshow.h>
#include <stdint.h>
#include <typeinfo>
static const GUID GUID_NULL = {}; // not defined in my headers, how lovely
DEFINE_GUID(CLSID_decodebin_parser, 0xf9d8d64e, 0xa144, 0x47dc, 0x8e, 0xe0, 0xf5, 0x34, 0x98, 0x37, 0x2c, 0x29);
static char* guid_to_str(const GUID& guid)
{
static char buf[8][64];
static int n = 0;
char* ret = buf[n++%8];
sprintf(ret, "{%08lX-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}", guid.Data1, guid.Data2, guid.Data3, guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]);
return ret;
}
static wchar_t* guid_to_wstr(const GUID& guid)
{
static wchar_t buf[8][64] = {};
char* guids = guid_to_str(guid);
static int n = 0;
wchar_t* ret = buf[n++%8];
for (int i=0;guids[i];i++)
ret[i] = guids[i];
return ret;
}
template<typename T> class CComPtr {
void assign(T* ptr)
{
p = ptr;
}
void release()
{
if (p)
p->Release();
p = nullptr;
}
public:
T* p;
CComPtr() { p = nullptr; }
~CComPtr() { release(); }
CComPtr(const CComPtr&) = delete;
CComPtr(CComPtr&&) = delete;
void operator=(const CComPtr&) = delete;
void operator=(CComPtr&&) = delete;
CComPtr& operator=(T* ptr)
{
release();
assign(ptr);
return *this;
}
T** operator&()
{
release();
return &p;
}
T* operator->() { return p; }
operator T*() { return p; }
HRESULT CoCreateInstance(REFCLSID rclsid, LPUNKNOWN pUnkOuter, DWORD dwClsContext)
{
release();
return ::CoCreateInstance(rclsid, pUnkOuter, dwClsContext, IID_PPV_ARGS(&p));
}
template<typename T2>
HRESULT QueryInterface(T2** other)
{
return p->QueryInterface(IID_PPV_ARGS(other));
}
};
static HRESULT connect_filters(IGraphBuilder* graph, IBaseFilter* src, IBaseFilter* dst)
{
puts("connect.");
CComPtr<IEnumPins> src_enum;
if (FAILED(src->EnumPins(&src_enum)))
return E_FAIL;
CComPtr<IPin> src_pin;
while (src_enum->Next(1, &src_pin, nullptr) == S_OK)
{
puts("src.");
PIN_INFO src_info;
if (FAILED(src_pin->QueryPinInfo(&src_info)))
return E_FAIL;
if (src_info.pFilter)
src_info.pFilter->Release();
if (src_info.dir != PINDIR_OUTPUT)
continue;
CComPtr<IPin> check_pin;
src_pin->ConnectedTo(&check_pin);
if (check_pin != nullptr)
continue;
CComPtr<IEnumPins> dst_enum;
dst->EnumPins(&dst_enum);
CComPtr<IPin> dst_pin;
while (dst_enum->Next(1, &dst_pin, nullptr) == S_OK)
{
puts("dst.");
PIN_INFO dst_info;
if (FAILED(dst_pin->QueryPinInfo(&dst_info)))
return E_FAIL;
if (dst_info.pFilter)
dst_info.pFilter->Release();
if (dst_info.dir != PINDIR_INPUT)
continue;
dst_pin->ConnectedTo(&check_pin);
if (check_pin != nullptr)
continue;
puts("match.");
//if (SUCCEEDED(graph->Connect(src_pin, dst_pin)))
if (SUCCEEDED(graph->ConnectDirect(src_pin, dst_pin, nullptr)))
{
puts("match2.");
return S_OK;
}
puts("match3.");
}
}
return E_FAIL;
}
static void require(int seq, HRESULT hr, const char * text = "")
{
if (SUCCEEDED(hr))
return;
printf("fail: %d %.8lX%s%s\n", seq, hr, text?" ":"", text);
Sleep(5000);
exit(seq);
}
static CComPtr<IGraphBuilder> filterGraph;
IBaseFilter* try_make_filter(REFIID riid)
{
IBaseFilter* filt = nullptr;
CoCreateInstance(riid, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&filt));
if (!filt)
return nullptr;
filterGraph->AddFilter(filt, guid_to_wstr(riid));
return filt;
}
IBaseFilter* make_filter(IBaseFilter* filt) { return filt; }
IBaseFilter* make_filter(REFIID riid)
{
IBaseFilter* filt = try_make_filter(riid);
if (!filt)
{
printf("failed to create %s\n", guid_to_str(riid));
Sleep(5000);
exit(1);
}
return filt;
}
bool connect_chain(IBaseFilter** filts, size_t n, bool required)
{
for (size_t i=0;i<n-1;i++)
{
IBaseFilter* first = filts[i];
IBaseFilter* second = filts[i+1];
if (FAILED(connect_filters(filterGraph, first, second)))
{
if (!required)
return false;
FILTER_INFO inf1;
FILTER_INFO inf2;
first->QueryFilterInfo(&inf1);
second->QueryFilterInfo(&inf2);
printf("failed to connect %ls to %ls\n", inf1.achName, inf2.achName);
Sleep(5000);
exit(1);
}
}
return true;
}
template<typename... Ts>
IBaseFilter* chain_tail(Ts... args)
{
IBaseFilter* filts[] = { make_filter(args)... };
connect_chain(filts, sizeof...(Ts), true);
return filts[sizeof...(Ts)-1];
}
template<typename... Ts>
void chain(Ts... args)
{
IBaseFilter* filts[] = { make_filter(args)... };
connect_chain(filts, sizeof...(Ts), true);
}
template<typename... Ts>
bool try_chain(Ts... args)
{
IBaseFilter* filts[] = { make_filter(args)... };
return connect_chain(filts, sizeof...(Ts), false);
}
int main()
{
setvbuf(stdout, nullptr, _IONBF, 0);
CoInitializeEx(NULL, COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE);
filterGraph.CoCreateInstance(CLSID_FilterGraph, nullptr, CLSCTX_INPROC_SERVER);
IBaseFilter* asyncReader = make_filter(CLSID_AsyncReader);
CComPtr<IFileSourceFilter> asyncReaderFsf;
require(20, asyncReader->QueryInterface(&asyncReaderFsf));
require(21, asyncReaderFsf->Load(L"video.mpg", nullptr), "failed to open video.mpg, does the file exist?");
//require(21, asyncReaderFsf->Load(L"Video1.WMV", nullptr), "failed to open Video1.WMV, does the file exist?");
IBaseFilter* mpegdec = try_make_filter(CLSID_CMpegVideoCodec);
//mpegdec = nullptr; // uncomment to force decodebin
bool audio;
if (mpegdec)
{
IBaseFilter* demux = chain_tail(asyncReader, CLSID_MPEG1Splitter);
chain(demux, mpegdec, CLSID_VideoMixingRenderer9);
audio = try_chain(demux, CLSID_CMpegAudioCodec, CLSID_DSoundRender); // don't worry too much if the file doesn't have sound
}
else
{
puts("CLSID_CMpegVideoCodec not available? Probably running on Wine, trying CLSID_decodebin_parser instead");
IBaseFilter* demux = chain_tail(asyncReader, CLSID_decodebin_parser);
puts("a");
chain(demux, CLSID_VideoMixingRenderer9);
puts("b");
audio = try_chain(demux, CLSID_DSoundRender);
puts("c");
}
if (!audio)
puts("no audio??");
puts("connected");
CComPtr<IMediaControl> filterGraph_mc;
require(40, filterGraph.QueryInterface(&filterGraph_mc));
filterGraph_mc->Run();
puts("running");
SleepEx(10000, true);
}