0
0
mirror of https://github.com/obsproject/obs-studio.git synced 2024-09-20 21:13:04 +02:00
obs-studio/plugins/win-dshow/virtualcam.c
jp9000 6377fe3177 win-dshow: Add Virtual Camera (Windows)
The virtual camera adds the ability to use the output of OBS itself as a
camera that can be selected within other Windows applications.  This is
very loosely based upon the catxfish virtual camera plugin design.

There is a shared memory queue, but instead of having 10-20 frames in
the queue, there are now only 3 frames in the queue to minimize latency
and reduce memory usage.  The third frame is mostly to ensure that
writing does not occur on the same frame being read; the delay is merely
one frame at all times.

The frames of the shared memory queue are NV12 instead of YUYV, which
reduces the memory and data copied, as well as eliminate unnecessary
conversion from NV12.  Some programs (such as chrome, which uses webrtc
to capture) do not support NV12 however, so an I420 conversion is
provided, which is far less expensive than YUYV.  The CPU cost of NV12
-> I420 is negligible in comparison.

The virtual camera filter itself is based upon the output filter within
the libdshowcapture library, which was originally implemented for other
purposes.  This is more ideal than the Microsoft example code because
for one, it's far less convoluted, two, allows us to be able to
customize the filter to our needs a bit more easily, and three, has much
better RAII.  The Microsoft CBaseFilter/etc code comprised of about 30
source files, where as the output filter comprises of two or three
required source files which we already had, so it's a huge win to
compile time.

Scaling is avoided whenever possible to minimize CPU usage.  When the
virtual camera is activated in OBS, the width, height, and frame
interval are saved, that way if the filter is activated, it will always
remember the last OBS resolution/interval that the virtual camera was
activated with, even if OBS is not active.  If for some reason the
filter activates before OBS starts up, and OBS starts up with a
different resolution, it will use simple point scaling intermittently,
and then will remember the new scaling in the future.  The scaler could
use some optimization.  FFmpeg was not opted for because the FFmpeg DLLs
would have to be provided for both architectures, which would be about
30 megabytes in total, and would make writing the plugin much more
painful.  Thus a simple point scaling algorithm is used, and scaling is
avoided whenever possible.

(If another willing participant wants to have a go at improving the
scaling then go for it.  But otherwise, it avoids scaling whenever
possible anyway, so it's not a huge deal)
2020-07-07 06:09:59 -07:00

103 lines
2.4 KiB
C

#include <obs-module.h>
#include <util/platform.h>
#include "shared-memory-queue.h"
struct virtualcam_data {
obs_output_t *output;
video_queue_t *vq;
};
static const char *virtualcam_name(void *unused)
{
UNUSED_PARAMETER(unused);
return "Virtual Camera Output";
}
static void virtualcam_destroy(void *data)
{
struct virtualcam_data *vcam = (struct virtualcam_data *)data;
video_queue_close(vcam->vq);
bfree(data);
}
static void *virtualcam_create(obs_data_t *settings, obs_output_t *output)
{
struct virtualcam_data *vcam =
(struct virtualcam_data *)bzalloc(sizeof(*vcam));
vcam->output = output;
UNUSED_PARAMETER(settings);
return vcam;
}
static bool virtualcam_start(void *data)
{
struct virtualcam_data *vcam = (struct virtualcam_data *)data;
uint32_t width = obs_output_get_width(vcam->output);
uint32_t height = obs_output_get_height(vcam->output);
struct obs_video_info ovi;
obs_get_video_info(&ovi);
uint64_t interval = ovi.fps_den * 10000000ULL / ovi.fps_num;
char res[64];
snprintf(res, sizeof(res), "%dx%dx%lld", (int)width, (int)height,
(long long)interval);
char *res_file = os_get_config_path_ptr("obs-virtualcam.txt");
os_quick_write_utf8_file_safe(res_file, res, strlen(res), false, "tmp",
NULL);
bfree(res_file);
vcam->vq = video_queue_create(width, height, interval);
if (!vcam->vq) {
blog(LOG_WARNING, "starting virtual-output failed");
return false;
}
struct video_scale_info vsi = {0};
vsi.format = VIDEO_FORMAT_NV12;
vsi.width = width;
vsi.height = height;
obs_output_set_video_conversion(vcam->output, &vsi);
blog(LOG_INFO, "Virtual output started");
obs_output_begin_data_capture(vcam->output, 0);
return true;
}
static void virtualcam_stop(void *data, uint64_t ts)
{
struct virtualcam_data *vcam = (struct virtualcam_data *)data;
obs_output_end_data_capture(vcam->output);
video_queue_close(vcam->vq);
vcam->vq = NULL;
blog(LOG_INFO, "Virtual output stopped");
UNUSED_PARAMETER(ts);
}
static void virtual_video(void *param, struct video_data *frame)
{
struct virtualcam_data *vcam = (struct virtualcam_data *)param;
if (!vcam->vq)
return;
video_queue_write(vcam->vq, frame->data, frame->linesize,
frame->timestamp);
}
struct obs_output_info virtualcam_info = {
.id = "virtualcam_output",
.flags = OBS_OUTPUT_VIDEO,
.get_name = virtualcam_name,
.create = virtualcam_create,
.destroy = virtualcam_destroy,
.start = virtualcam_start,
.stop = virtualcam_stop,
.raw_video = virtual_video,
};