-
Notifications
You must be signed in to change notification settings - Fork 180
/
dshow-encoded-device.cpp
260 lines (217 loc) · 6.92 KB
/
dshow-encoded-device.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
254
255
256
257
258
259
260
/*
* Copyright (C) 2023 Lain Bailey <lain@obsproject.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#include "dshow-base.hpp"
#include "dshow-media-type.hpp"
#include "dshow-formats.hpp"
#include "dshow-demux.hpp"
#include "capture-filter.hpp"
#include "device.hpp"
#include "log.hpp"
namespace DShow {
static inline bool CreateFilters(IBaseFilter *filter, IBaseFilter **crossbar,
IBaseFilter **encoder, IBaseFilter **demuxer)
{
ComPtr<IPin> inputPin;
ComPtr<IPin> outputPin;
REGPINMEDIUM inMedium;
REGPINMEDIUM outMedium;
bool hasOutMedium;
HRESULT hr;
if (!GetPinByName(filter, PINDIR_INPUT, nullptr, &inputPin)) {
Warning(L"Encoded Device: Failed to get input pin");
return false;
}
if (!GetPinByName(filter, PINDIR_OUTPUT, nullptr, &outputPin)) {
Warning(L"Encoded Device: Failed to get output pin");
return false;
}
if (!GetPinMedium(inputPin, inMedium)) {
Warning(L"Encoded Device: Failed to get input pin medium");
return false;
}
hasOutMedium = GetPinMedium(outputPin, outMedium);
if (!GetFilterByMedium(AM_KSCATEGORY_CROSSBAR, inMedium, crossbar)) {
Warning(L"Encoded Device: Failed to get crossbar filter");
return false;
}
/* perfectly okay if there's no encoder filter, some don't have them */
if (hasOutMedium)
GetFilterByMedium(KSCATEGORY_ENCODER, outMedium, encoder);
hr = CoCreateInstance(CLSID_MPEG2Demultiplexer, nullptr,
CLSCTX_INPROC_SERVER, IID_IBaseFilter,
(void **)demuxer);
if (FAILED(hr)) {
WarningHR(L"Encoded Device: Failed to create demuxer", hr);
return false;
}
return true;
}
static inline bool ConnectEncodedFilters(IGraphBuilder *graph,
IBaseFilter *filter,
IBaseFilter *crossbar,
IBaseFilter *encoder,
IBaseFilter *demuxer)
{
if (!DirectConnectFilters(graph, crossbar, filter)) {
Warning(L"Encoded Device: Failed to connect crossbar to "
L"device");
return false;
}
if (!!encoder) {
if (!DirectConnectFilters(graph, filter, encoder)) {
Warning(L"Encoded Device: Failed to connect device to "
L"encoder");
return false;
}
if (!DirectConnectFilters(graph, encoder, demuxer)) {
Warning(L"Encoded Device: Failed to connect encoder to "
L"demuxer");
return false;
}
} else {
if (!DirectConnectFilters(graph, filter, demuxer)) {
Warning(L"Encoded Device: Failed to connect device to "
L"demuxer");
return false;
}
}
return true;
}
static inline bool MapPacketIDs(IBaseFilter *demuxer, ULONG video, ULONG audio)
{
ComPtr<IPin> videoPin, audioPin;
HRESULT hr;
if (!GetPinByName(demuxer, PINDIR_OUTPUT, DEMUX_VIDEO_PIN, &videoPin)) {
Warning(L"Encoded Device: Could not get video pin from "
L"demuxer");
return false;
}
if (!GetPinByName(demuxer, PINDIR_OUTPUT, DEMUX_AUDIO_PIN, &audioPin)) {
Warning(L"Encoded Device: Could not get audio pin from "
L"demuxer");
return false;
}
hr = MapPinToPacketID(videoPin, video);
if (FAILED(hr)) {
WarningHR(L"Encoded Device: Failed to map demuxer video pin "
L"packet ID",
hr);
return false;
}
hr = MapPinToPacketID(audioPin, audio);
if (FAILED(hr)) {
WarningHR(L"Encoded Device: Failed to map demuxer audio pin "
L"packet ID",
hr);
return false;
}
return true;
}
/*
* rocket-specific workaround code. I have no idea what any of these numbers
* are except the GUID which was obvious. All I know is calling
* IKsPropertySet::Set turns on/off some sort of 'mode' on the device.
* I discovered this merely by chance while monitoring API usage in other
* programs because I could not figure out how the hell to get this thing
* to turn on.
*/
static const GUID RocketEncoderGUID = {0x99100000,
0xa330,
0x11e1,
{0xa3, 0x80, 0x99, 0x10, 0x68, 0x64,
0x00, 0x00}};
struct RocketPropStruct {
DWORD dwSize;
DWORD unknown1;
DWORD unknown2;
DWORD unknown3;
DWORD code;
DWORD unknown4;
BOOL enabled;
};
struct RocketInstStruct {
DWORD code;
DWORD unknown1;
};
bool SetRocketEnabled(IBaseFilter *encoder, bool enable)
{
static const ULONG rocketEnableId = 0x9910E001;
static const DWORD rocketEnableCode = 0x38384001;
RocketInstStruct rocketInstance = {};
RocketPropStruct rocketProperty = {};
ComQIPtr<IKsPropertySet> propertySet(encoder);
if (!propertySet)
return false;
rocketProperty.dwSize = sizeof(rocketProperty);
rocketProperty.code = rocketEnableCode;
rocketProperty.enabled = enable;
rocketInstance.code = rocketEnableCode;
HRESULT hr = propertySet->Set(RocketEncoderGUID, rocketEnableId,
&rocketInstance, sizeof(rocketInstance),
&rocketProperty, sizeof(rocketProperty));
return SUCCEEDED(hr);
}
bool HDevice::SetupEncodedVideoCapture(IBaseFilter *filter, VideoConfig &config,
const EncodedDevice &info)
{
ComPtr<IBaseFilter> crossbar;
ComPtr<IBaseFilter> encoder;
ComPtr<IBaseFilter> demuxer;
MediaType mtVideo;
MediaType mtAudio;
if (!CreateFilters(filter, &crossbar, &encoder, &demuxer))
return false;
if (!CreateDemuxVideoPin(demuxer, mtVideo, info.width, info.height,
info.frameInterval, info.videoFormat))
return false;
if (!CreateDemuxAudioPin(demuxer, mtAudio, info.samplesPerSec, 16, 2,
info.audioFormat))
return false;
config.cx = info.width;
config.cy_abs = labs(info.height);
config.cy_flip = info.height < 0;
config.frameInterval = info.frameInterval;
config.format = info.videoFormat;
config.internalFormat = info.videoFormat;
PinCaptureInfo pci;
pci.callback = [this](IMediaSample *s) { Receive(true, s); };
pci.expectedMajorType = mtVideo->majortype;
pci.expectedSubType = mtVideo->subtype;
videoCapture = new CaptureFilter(pci);
videoFilter = demuxer;
if (!!encoder && config.name.find(L"IT9910") != std::string::npos) {
rocketEncoder = encoder;
if (!SetRocketEnabled(rocketEncoder, true))
return false;
}
graph->AddFilter(crossbar, L"Crossbar");
graph->AddFilter(filter, L"Device");
graph->AddFilter(demuxer, L"Demuxer");
graph->AddFilter(videoCapture, L"Capture Filter");
if (!!encoder)
graph->AddFilter(encoder, L"Encoder");
bool success = ConnectEncodedFilters(graph, filter, crossbar, encoder,
demuxer);
if (success)
success = MapPacketIDs(demuxer, info.videoPacketID,
info.audioPacketID);
encodedDevice = success;
return success;
}
}; /* namespace DShow */