-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
virtual-cam-filter.c
329 lines (292 loc) · 9.36 KB
/
virtual-cam-filter.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <obs-module.h>
#include <../UI/obs-frontend-api/obs-frontend-api.h>
#include <util/threading.h>
#include "version.h"
#include "util/platform.h"
struct virtual_cam_filter_context {
obs_source_t *source;
obs_output_t *virtualCam;
uint8_t *video_data;
uint32_t video_linesize;
video_t *video_output;
bool output_active;
uint32_t width;
uint32_t height;
gs_texrender_t *texrender;
gs_stagesurf_t *stagesurface;
struct obs_video_info ovi;
uint64_t last_failed_start;
bool restart;
bool stopping;
bool starting;
bool started;
};
static const char *virtual_cam_filter_source_get_name(void *unused)
{
UNUSED_PARAMETER(unused);
return "Virtual Camera";
}
struct video_frame {
uint8_t *data[MAX_AV_PLANES];
uint32_t linesize[MAX_AV_PLANES];
};
static void *start_output_thread(void *data)
{
struct virtual_cam_filter_context *filter = data;
if (obs_output_start(filter->virtualCam)) {
filter->output_active = true;
obs_source_inc_showing(obs_filter_get_parent(filter->source));
} else {
filter->last_failed_start = time;
}
filter->starting = false;
return NULL;
}
void virtual_cam_filter_offscreen_render(void *data, uint32_t cx, uint32_t cy)
{
struct virtual_cam_filter_context *filter = data;
obs_source_t *parent = obs_filter_get_parent(filter->source);
if (!parent || obs_source_removed(parent)) {
return;
}
const uint32_t width = obs_source_get_width(parent);
const uint32_t height = obs_source_get_height(parent);
if (!width || !height)
return;
gs_texrender_reset(filter->texrender);
if (!gs_texrender_begin(filter->texrender, width, height))
return;
struct vec4 background;
vec4_zero(&background);
gs_clear(GS_CLEAR_COLOR, &background, 0.0f, 0);
gs_ortho(0.0f, (float)width, 0.0f, (float)height, -100.0f, 100.0f);
gs_blend_state_push();
gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
obs_source_video_render(parent);
gs_blend_state_pop();
gs_texrender_end(filter->texrender);
if (filter->width != width || filter->height != height) {
gs_stagesurface_destroy(filter->stagesurface);
filter->stagesurface =
gs_stagesurface_create(width, height, GS_BGRA);
struct video_output_info vi = {0};
vi.format = VIDEO_FORMAT_BGRA;
vi.width = width;
vi.height = height;
vi.fps_den = filter->ovi.fps_den;
vi.fps_num = filter->ovi.fps_num;
vi.cache_size = 16;
vi.colorspace = VIDEO_CS_DEFAULT;
vi.range = VIDEO_RANGE_DEFAULT;
vi.name = obs_source_get_name(filter->source);
video_t *o = filter->video_output;
filter->video_output = NULL;
if (o) {
video_output_stop(o);
video_output_close(o);
}
if (video_output_open(&filter->video_output, &vi) ==
VIDEO_OUTPUT_SUCCESS) {
filter->width = width;
filter->height = height;
filter->restart = true;
}
}
if (!filter->video_output || video_output_stopped(filter->video_output))
return;
if (!filter->output_active && obs_source_enabled(filter->source)) {
const uint64_t time = obs_get_video_frame_time();
if (time - filter->last_failed_start < 1000000)
return;
obs_output_set_media(filter->virtualCam, filter->video_output,
NULL);
if (!filter->starting) {
filter->starting = true;
pthread_t thread;
pthread_create(&thread, NULL, start_output_thread,
filter);
}
} else if (filter->output_active &&
!obs_source_enabled(filter->source)) {
obs_output_stop(filter->virtualCam);
filter->output_active = false;
obs_source_dec_showing(obs_filter_get_parent(filter->source));
}
struct video_frame output_frame;
if (!video_output_lock_frame(filter->video_output, &output_frame, 1,
obs_get_video_frame_time()))
return;
if (filter->video_data) {
gs_stagesurface_unmap(filter->stagesurface);
filter->video_data = NULL;
}
if (!filter->stagesurface)
filter->stagesurface =
gs_stagesurface_create(width, height, GS_BGRA);
gs_stage_texture(filter->stagesurface,
gs_texrender_get_texture(filter->texrender));
gs_stagesurface_map(filter->stagesurface, &filter->video_data,
&filter->video_linesize);
if (filter->video_data && filter->video_linesize) {
const uint32_t linesize = output_frame.linesize[0];
for (uint32_t i = 0; i < filter->height; ++i) {
const uint32_t dst_offset = linesize * i;
const uint32_t src_offset = filter->video_linesize * i;
memcpy(output_frame.data[0] + dst_offset,
filter->video_data + src_offset, linesize);
}
}
video_output_unlock_frame(filter->video_output);
}
static void virtual_cam_filter_source_defaults(obs_data_t *settings) {}
static void frontend_event(enum obs_frontend_event event, void *data)
{
struct virtual_cam_filter_context *context = data;
if (event == OBS_FRONTEND_EVENT_EXIT ||
event == OBS_FRONTEND_EVENT_SCENE_COLLECTION_CLEANUP) {
if (context->stopping)
return;
context->stopping = true;
if (context->output_active) {
context->output_active = false;
obs_source_dec_showing(
obs_filter_get_parent(context->source));
}
obs_remove_main_render_callback(
virtual_cam_filter_offscreen_render, context);
obs_output_force_stop(context->virtualCam);
obs_output_end_data_capture(context->virtualCam);
obs_output_release(context->virtualCam);
context->virtualCam = NULL;
video_t *o = context->video_output;
context->video_output = NULL;
if (o) {
video_output_stop(o);
video_output_close(o);
}
}
}
static void *virtual_cam_filter_source_create(obs_data_t *settings,
obs_source_t *source)
{
struct virtual_cam_filter_context *context =
bzalloc(sizeof(struct virtual_cam_filter_context));
context->source = source;
context->virtualCam = obs_output_create(
"virtualcam_output", "virtualcam_output_filter", NULL, NULL);
context->texrender = gs_texrender_create(GS_BGRA, GS_ZS_NONE);
obs_get_video_info(&context->ovi);
obs_frontend_add_event_callback(frontend_event, context);
return context;
}
static void virtual_cam_filter_source_destroy(void *data)
{
struct virtual_cam_filter_context *context = data;
obs_frontend_remove_event_callback(frontend_event, context);
if (!context->stopping) {
context->stopping = true;
if (context->output_active) {
context->output_active = false;
obs_source_dec_showing(
obs_filter_get_parent(context->source));
}
obs_remove_main_render_callback(
virtual_cam_filter_offscreen_render, context);
obs_output_force_stop(context->virtualCam);
obs_output_end_data_capture(context->virtualCam);
obs_output_release(context->virtualCam);
}
video_t *o = context->video_output;
context->video_output = NULL;
if (o) {
video_output_stop(o);
video_output_close(o);
}
gs_stagesurface_unmap(context->stagesurface);
gs_stagesurface_destroy(context->stagesurface);
gs_texrender_destroy(context->texrender);
bfree(context);
}
static void virtual_cam_filter_source_tick(void *data, float seconds)
{
struct virtual_cam_filter_context *context = data;
if (context->stopping)
return;
if (!context->started) {
obs_add_main_render_callback(
virtual_cam_filter_offscreen_render, context);
context->started = true;
}
if (context->restart && context->output_active) {
obs_output_stop(context->virtualCam);
context->output_active = false;
context->restart = false;
obs_source_dec_showing(obs_filter_get_parent(context->source));
} else if (!context->output_active && context->video_output &&
obs_source_enabled(context->source)) {
uint64_t time = obs_get_video_frame_time();
if (time - context->last_failed_start < 1000000)
return;
obs_output_set_media(context->virtualCam, context->video_output,
NULL);
if (!context->starting) {
context->starting = true;
pthread_t thread;
pthread_create(&thread, NULL, start_output_thread,
context);
}
} else if (context->output_active &&
!obs_source_enabled(context->source)) {
obs_output_stop(context->virtualCam);
context->output_active = false;
obs_source_dec_showing(obs_filter_get_parent(context->source));
}
}
static obs_properties_t *virtual_cam_filter_source_properties(void *data)
{
struct virtual_cam_filter_context *s = data;
obs_properties_t *props = obs_properties_create();
return props;
}
void virtual_cam_filter_source_render(void *data, gs_effect_t *effect)
{
UNUSED_PARAMETER(effect);
struct virtual_cam_filter_context *context = data;
obs_source_skip_video_filter(context->source);
}
static void virtual_cam_filter_source_filter_remove(void *data,
obs_source_t *parent)
{
struct virtual_cam_filter_context *context = data;
if (context->output_active) {
obs_output_stop(context->virtualCam);
context->output_active = false;
obs_source_dec_showing(parent);
}
}
struct obs_source_info virtual_cam_filter_info = {
.id = "virtual_cam_filter",
.type = OBS_SOURCE_TYPE_FILTER,
.output_flags = OBS_SOURCE_VIDEO,
.get_name = virtual_cam_filter_source_get_name,
.create = virtual_cam_filter_source_create,
.destroy = virtual_cam_filter_source_destroy,
.get_defaults = virtual_cam_filter_source_defaults,
.video_render = virtual_cam_filter_source_render,
.video_tick = virtual_cam_filter_source_tick,
.get_properties = virtual_cam_filter_source_properties,
.filter_remove = virtual_cam_filter_source_filter_remove,
};
OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("virtual-cam-filter", "en-US")
MODULE_EXPORT const char *obs_module_description(void)
{
return "Virtual Camera Filter";
}
bool obs_module_load(void)
{
blog(LOG_INFO, "[Virtual Cam Filter] loaded version %s",
PROJECT_VERSION);
obs_register_source(&virtual_cam_filter_info);
return true;
}