-
-
Notifications
You must be signed in to change notification settings - Fork 8k
/
monitor-capture.c
235 lines (177 loc) · 6.13 KB
/
monitor-capture.c
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
#include <util/dstr.h>
#include "dc-capture.h"
/* clang-format off */
#define TEXT_MONITOR_CAPTURE obs_module_text("MonitorCapture")
#define TEXT_CAPTURE_CURSOR obs_module_text("CaptureCursor")
#define TEXT_COMPATIBILITY obs_module_text("Compatibility")
#define TEXT_MONITOR obs_module_text("Monitor")
#define TEXT_PRIMARY_MONITOR obs_module_text("PrimaryMonitor")
/* clang-format on */
struct monitor_capture {
obs_source_t *source;
int monitor;
bool capture_cursor;
bool compatibility;
struct dc_capture data;
};
struct monitor_info {
int cur_id;
int desired_id;
int id;
RECT rect;
};
/* ------------------------------------------------------------------------- */
static inline void do_log(int level, const char *msg, ...)
{
va_list args;
struct dstr str = {0};
va_start(args, msg);
dstr_copy(&str, "[GDI monitor capture]: ");
dstr_vcatf(&str, msg, args);
blog(level, "%s", str.array);
dstr_free(&str);
va_end(args);
}
static BOOL CALLBACK enum_monitor(HMONITOR handle, HDC hdc, LPRECT rect, LPARAM param)
{
struct monitor_info *monitor = (struct monitor_info *)param;
if (monitor->cur_id == 0 || monitor->desired_id == monitor->cur_id) {
monitor->rect = *rect;
monitor->id = monitor->cur_id;
}
UNUSED_PARAMETER(hdc);
UNUSED_PARAMETER(handle);
return (monitor->desired_id > monitor->cur_id++);
}
static void update_monitor(struct monitor_capture *capture, obs_data_t *settings)
{
struct monitor_info monitor = {0};
uint32_t width, height;
monitor.desired_id = (int)obs_data_get_int(settings, "monitor");
EnumDisplayMonitors(NULL, NULL, enum_monitor, (LPARAM)&monitor);
capture->monitor = monitor.id;
width = monitor.rect.right - monitor.rect.left;
height = monitor.rect.bottom - monitor.rect.top;
dc_capture_init(&capture->data, monitor.rect.left, monitor.rect.top, width, height, capture->capture_cursor,
capture->compatibility);
}
static inline void update_settings(struct monitor_capture *capture, obs_data_t *settings)
{
capture->monitor = (int)obs_data_get_int(settings, "monitor");
capture->capture_cursor = obs_data_get_bool(settings, "capture_cursor");
capture->compatibility = obs_data_get_bool(settings, "compatibility");
dc_capture_free(&capture->data);
update_monitor(capture, settings);
}
/* ------------------------------------------------------------------------- */
static const char *monitor_capture_getname(void *unused)
{
UNUSED_PARAMETER(unused);
return TEXT_MONITOR_CAPTURE;
}
static void monitor_capture_destroy(void *data)
{
struct monitor_capture *capture = data;
obs_enter_graphics();
dc_capture_free(&capture->data);
obs_leave_graphics();
bfree(capture);
}
static void monitor_capture_defaults(obs_data_t *settings)
{
obs_data_set_default_int(settings, "monitor", 0);
obs_data_set_default_bool(settings, "capture_cursor", true);
obs_data_set_default_bool(settings, "compatibility", false);
}
static void monitor_capture_update(void *data, obs_data_t *settings)
{
struct monitor_capture *mc = data;
update_settings(mc, settings);
}
static void *monitor_capture_create(obs_data_t *settings, obs_source_t *source)
{
struct monitor_capture *capture;
capture = bzalloc(sizeof(struct monitor_capture));
capture->source = source;
update_settings(capture, settings);
return capture;
}
static void monitor_capture_tick(void *data, float seconds)
{
struct monitor_capture *capture = data;
if (!obs_source_showing(capture->source))
return;
obs_enter_graphics();
dc_capture_capture(&capture->data, NULL);
obs_leave_graphics();
UNUSED_PARAMETER(seconds);
}
static void monitor_capture_render(void *data, gs_effect_t *effect)
{
struct monitor_capture *capture = data;
dc_capture_render(&capture->data, obs_source_get_texcoords_centered(capture->source));
UNUSED_PARAMETER(effect);
}
static uint32_t monitor_capture_width(void *data)
{
struct monitor_capture *capture = data;
return capture->data.width;
}
static uint32_t monitor_capture_height(void *data)
{
struct monitor_capture *capture = data;
return capture->data.height;
}
static BOOL CALLBACK enum_monitor_props(HMONITOR handle, HDC hdc, LPRECT rect, LPARAM param)
{
UNUSED_PARAMETER(hdc);
UNUSED_PARAMETER(rect);
obs_property_t *monitor_list = (obs_property_t *)param;
MONITORINFO mi;
size_t monitor_id = 0;
struct dstr monitor_desc = {0};
struct dstr resolution = {0};
struct dstr format_string = {0};
monitor_id = obs_property_list_item_count(monitor_list);
mi.cbSize = sizeof(mi);
GetMonitorInfo(handle, &mi);
dstr_catf(&resolution, "%dx%d @ %d,%d", mi.rcMonitor.right - mi.rcMonitor.left,
mi.rcMonitor.bottom - mi.rcMonitor.top, mi.rcMonitor.left, mi.rcMonitor.top);
dstr_copy(&format_string, "%s %d: %s");
if (mi.dwFlags == MONITORINFOF_PRIMARY) {
dstr_catf(&format_string, " (%s)", TEXT_PRIMARY_MONITOR);
}
dstr_catf(&monitor_desc, format_string.array, TEXT_MONITOR, monitor_id + 1, resolution.array);
obs_property_list_add_int(monitor_list, monitor_desc.array, (int)monitor_id);
dstr_free(&monitor_desc);
dstr_free(&resolution);
dstr_free(&format_string);
return TRUE;
}
static obs_properties_t *monitor_capture_properties(void *unused)
{
UNUSED_PARAMETER(unused);
obs_properties_t *props = obs_properties_create();
obs_property_t *monitors =
obs_properties_add_list(props, "monitor", TEXT_MONITOR, OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
obs_properties_add_bool(props, "compatibility", TEXT_COMPATIBILITY);
obs_properties_add_bool(props, "capture_cursor", TEXT_CAPTURE_CURSOR);
EnumDisplayMonitors(NULL, NULL, enum_monitor_props, (LPARAM)monitors);
return props;
}
struct obs_source_info monitor_capture_info = {
.id = "monitor_capture",
.type = OBS_SOURCE_TYPE_INPUT,
.output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW | OBS_SOURCE_DO_NOT_DUPLICATE | OBS_SOURCE_SRGB,
.get_name = monitor_capture_getname,
.create = monitor_capture_create,
.destroy = monitor_capture_destroy,
.video_render = monitor_capture_render,
.video_tick = monitor_capture_tick,
.update = monitor_capture_update,
.get_width = monitor_capture_width,
.get_height = monitor_capture_height,
.get_defaults = monitor_capture_defaults,
.get_properties = monitor_capture_properties,
.icon_type = OBS_ICON_TYPE_DESKTOP_CAPTURE,
};