forked from obsproject/libdshowcapture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdshowcapture.cpp
313 lines (257 loc) · 7.29 KB
/
dshowcapture.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*
* Copyright (C) 2014 Hugh Bailey <obs.jim@gmail.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 "../dshowcapture.hpp"
#include "dshow-base.hpp"
#include "dshow-enum.hpp"
#include "device.hpp"
#include "dshow-device-defs.hpp"
#include "log.hpp"
#include <vector>
namespace DShow {
Device::Device(InitGraph initialize) : context(new HDevice)
{
if (initialize == InitGraph::True)
context->CreateGraph();
}
Device::~Device()
{
delete context;
}
bool Device::Valid() const
{
return context->initialized;
}
bool Device::ResetGraph()
{
/* cheap and easy way to clear all the filters */
delete context;
context = new HDevice;
return context->CreateGraph();
}
void Device::ShutdownGraph()
{
delete context;
context = new HDevice;
}
bool Device::SetVideoConfig(VideoConfig *config)
{
return context->SetVideoConfig(config);
}
bool Device::SetAudioConfig(AudioConfig *config)
{
return context->SetAudioConfig(config);
}
bool Device::ConnectFilters()
{
return context->ConnectFilters();
}
Result Device::Start()
{
return context->Start();
}
void Device::Stop()
{
context->Stop();
}
bool Device::GetVideoConfig(VideoConfig &config) const
{
if (context->videoCapture == NULL)
return false;
config = context->videoConfig;
return true;
}
bool Device::GetAudioConfig(AudioConfig &config) const
{
if (context->audioCapture == NULL)
return false;
config = context->audioConfig;
return true;
}
bool Device::GetVideoDeviceId(DeviceId &id) const
{
if (context->videoCapture == NULL)
return false;
id = context->videoConfig;
return true;
}
bool Device::GetAudioDeviceId(DeviceId &id) const
{
if (context->audioCapture == NULL)
return false;
id = context->audioConfig;
return true;
}
static void OpenPropertyPages(HWND hwnd, IUnknown *propertyObject)
{
if (!propertyObject)
return;
ComQIPtr<ISpecifyPropertyPages> pages(propertyObject);
CAUUID cauuid;
if (pages != NULL) {
if (SUCCEEDED(pages->GetPages(&cauuid)) && cauuid.cElems) {
OleCreatePropertyFrame(hwnd, 0, 0, NULL, 1,
(LPUNKNOWN *)&propertyObject,
cauuid.cElems, cauuid.pElems, 0,
0, NULL);
CoTaskMemFree(cauuid.pElems);
}
}
}
void Device::OpenDialog(void *hwnd, DialogType type) const
{
ComPtr<IUnknown> ptr;
HRESULT hr;
if (type == DialogType::ConfigVideo) {
ptr = context->videoFilter;
} else if (type == DialogType::ConfigCrossbar ||
type == DialogType::ConfigCrossbar2) {
hr = context->builder->FindInterface(NULL, NULL,
context->videoFilter,
IID_IAMCrossbar,
(void **)&ptr);
if (FAILED(hr)) {
WarningHR(L"Failed to find crossbar", hr);
return;
}
if (ptr != NULL && type == DialogType::ConfigCrossbar2) {
ComQIPtr<IAMCrossbar> xbar(ptr);
ComQIPtr<IBaseFilter> filter(xbar);
hr = context->builder->FindInterface(
&LOOK_UPSTREAM_ONLY, NULL, filter,
IID_IAMCrossbar, (void **)&ptr);
if (FAILED(hr)) {
WarningHR(L"Failed to find crossbar2", hr);
return;
}
}
} else if (type == DialogType::ConfigAudio) {
ptr = context->audioFilter;
}
if (!ptr) {
Warning(L"Could not find filter to open dialog type: %d with",
(int)type);
return;
}
OpenPropertyPages((HWND)hwnd, ptr);
}
static void EnumEncodedVideo(std::vector<VideoDevice> &devices,
const wchar_t *deviceName,
const wchar_t *devicePath,
const EncodedDevice &info)
{
VideoDevice device;
VideoInfo caps;
device.name = deviceName;
device.path = devicePath;
device.audioAttached = true;
device.separateAudioFilter = false;
caps.minCX = caps.maxCX = info.width;
caps.minCY = caps.maxCY = info.height;
caps.granularityCX = caps.granularityCY = 1;
caps.minInterval = caps.maxInterval = info.frameInterval;
caps.format = info.videoFormat;
device.caps.push_back(caps);
devices.push_back(device);
}
static void EnumExceptionVideoDevice(std::vector<VideoDevice> &devices,
IBaseFilter *filter,
const wchar_t *deviceName,
const wchar_t *devicePath)
{
ComPtr<IPin> pin;
if (GetPinByName(filter, PINDIR_OUTPUT, L"656", &pin))
EnumEncodedVideo(devices, deviceName, devicePath, HD_PVR2);
else if (GetPinByName(filter, PINDIR_OUTPUT, L"TS Out", &pin))
EnumEncodedVideo(devices, deviceName, devicePath, Roxio);
}
static bool EnumVideoDevice(std::vector<VideoDevice> &devices,
IBaseFilter *filter, const wchar_t *deviceName,
const wchar_t *devicePath)
{
ComPtr<IPin> pin;
ComPtr<IPin> audioPin;
ComPtr<IBaseFilter> audioFilter;
VideoDevice info;
if (wcsstr(deviceName, L"C875") != nullptr ||
wcsstr(deviceName, L"Prif Streambox") != nullptr ||
wcsstr(deviceName, L"C835") != nullptr) {
EnumEncodedVideo(devices, deviceName, devicePath, AV_LGP);
return true;
} else if (wcsstr(deviceName, L"Hauppauge HD PVR Capture") != nullptr) {
EnumEncodedVideo(devices, deviceName, devicePath, HD_PVR1);
return true;
}
bool success = GetFilterPin(filter, MEDIATYPE_Video,
PIN_CATEGORY_CAPTURE, PINDIR_OUTPUT, &pin);
/* if this device has no standard capture pin, see if it's an
* encoded device, and get its information if so (all encoded devices
* are exception devices pretty much) */
if (!success) {
EnumExceptionVideoDevice(devices, filter, deviceName,
devicePath);
return true;
}
if (!EnumVideoCaps(pin, info.caps))
return true;
info.audioAttached = GetFilterPin(filter, MEDIATYPE_Audio,
PIN_CATEGORY_CAPTURE, PINDIR_OUTPUT,
&audioPin);
// Fallback: Find a corresponding audio filter for the same device
if (!info.audioAttached) {
info.separateAudioFilter =
GetDeviceAudioFilter(devicePath, &audioFilter);
info.audioAttached = info.separateAudioFilter;
}
info.name = deviceName;
if (devicePath)
info.path = devicePath;
devices.push_back(info);
return true;
}
bool Device::EnumVideoDevices(std::vector<VideoDevice> &devices)
{
devices.clear();
return EnumDevices(CLSID_VideoInputDeviceCategory,
EnumDeviceCallback(EnumVideoDevice), &devices);
}
static bool EnumAudioDevice(vector<AudioDevice> &devices, IBaseFilter *filter,
const wchar_t *deviceName,
const wchar_t *devicePath)
{
ComPtr<IPin> pin;
AudioDevice info;
bool success = GetFilterPin(filter, MEDIATYPE_Audio,
PIN_CATEGORY_CAPTURE, PINDIR_OUTPUT, &pin);
if (!success)
return true;
if (!EnumAudioCaps(pin, info.caps))
return true;
info.name = deviceName;
if (devicePath)
info.path = devicePath;
devices.push_back(info);
return true;
}
bool Device::EnumAudioDevices(vector<AudioDevice> &devices)
{
devices.clear();
return EnumDevices(CLSID_AudioInputDeviceCategory,
EnumDeviceCallback(EnumAudioDevice), &devices);
}
}; /* namespace DShow */