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)
This commit is contained in:
78
plugins/win-dshow/virtualcam-module/CMakeLists.txt
Normal file
78
plugins/win-dshow/virtualcam-module/CMakeLists.txt
Normal file
@@ -0,0 +1,78 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
project(obs-virtualcam-module)
|
||||
|
||||
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
|
||||
set(_output_suffix "64")
|
||||
else()
|
||||
set(_output_suffix "32")
|
||||
endif()
|
||||
|
||||
configure_file(
|
||||
"${CMAKE_CURRENT_SOURCE_DIR}/virtualcam-module.def.in"
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/virtualcam-module.def")
|
||||
|
||||
set(libdshowcapture_SOURCES
|
||||
../libdshowcapture/source/log.cpp
|
||||
../libdshowcapture/source/dshow-base.cpp
|
||||
../libdshowcapture/source/dshow-enum.cpp
|
||||
../libdshowcapture/source/dshow-formats.cpp
|
||||
../libdshowcapture/source/dshow-media-type.cpp
|
||||
../libdshowcapture/source/output-filter.cpp
|
||||
)
|
||||
|
||||
set(libdshowcapture_HEADERS
|
||||
../libdshowcapture/source/ComPtr.hpp
|
||||
../libdshowcapture/source/CoTaskMemPtr.hpp
|
||||
../libdshowcapture/source/log.hpp
|
||||
../libdshowcapture/source/dshow-base.hpp
|
||||
../libdshowcapture/source/dshow-enum.hpp
|
||||
../libdshowcapture/source/dshow-formats.hpp
|
||||
../libdshowcapture/source/dshow-media-type.hpp
|
||||
../libdshowcapture/source/output-filter.hpp
|
||||
../libdshowcapture/dshowcapture.hpp
|
||||
)
|
||||
|
||||
set(obs-virtualcam-module_SOURCES
|
||||
"${CMAKE_CURRENT_BINARY_DIR}/virtualcam-module.def"
|
||||
sleepto.c
|
||||
placeholder.cpp
|
||||
virtualcam-module.cpp
|
||||
virtualcam-filter.cpp
|
||||
../shared-memory-queue.c
|
||||
../tiny-nv12-scale.c
|
||||
)
|
||||
|
||||
set(obs-virtualcam-module_HEADERS
|
||||
sleepto.h
|
||||
virtualcam-filter.hpp
|
||||
../shared-memory-queue.h
|
||||
../tiny-nv12-scale.h
|
||||
)
|
||||
|
||||
if(MSVC)
|
||||
add_compile_options("$<IF:$<CONFIG:Debug>,/MTd,/MT>")
|
||||
endif()
|
||||
|
||||
include_directories(${CMAKE_SOURCE_DIR}/libobs/util)
|
||||
|
||||
source_group("libdshowcapture\\Source Files" FILES ${libdshowcapture_SOURCES})
|
||||
source_group("libdshowcapture\\Header Files" FILES ${libdshowcapture_HEADERS})
|
||||
|
||||
set(CMAKE_MODULE_LINKER_FLAGS "${MAKE_MODULE_LINKER_FLAGS} /ignore:4104")
|
||||
|
||||
add_library(obs-virtualcam-module MODULE
|
||||
${libdshowcapture_SOURCES}
|
||||
${libdshowcapture_HEADERS}
|
||||
${obs-virtualcam-module_SOURCES}
|
||||
${obs-virtualcam-module_HEADERS})
|
||||
target_link_libraries(obs-virtualcam-module
|
||||
winmm
|
||||
strmiids
|
||||
gdiplus
|
||||
)
|
||||
set_target_properties(obs-virtualcam-module PROPERTIES FOLDER "plugins/win-dshow")
|
||||
|
||||
set_target_properties(obs-virtualcam-module
|
||||
PROPERTIES
|
||||
OUTPUT_NAME "obs-virtualcam-module${_output_suffix}")
|
||||
install_obs_datatarget(obs-virtualcam-module "obs-plugins/win-dshow")
|
146
plugins/win-dshow/virtualcam-module/placeholder.cpp
Normal file
146
plugins/win-dshow/virtualcam-module/placeholder.cpp
Normal file
@@ -0,0 +1,146 @@
|
||||
#include <windows.h>
|
||||
#include <strsafe.h>
|
||||
#include <gdiplus.h>
|
||||
#include <stdint.h>
|
||||
#include <vector>
|
||||
|
||||
using namespace Gdiplus;
|
||||
|
||||
extern HINSTANCE dll_inst;
|
||||
static std::vector<uint8_t> placeholder;
|
||||
|
||||
/* XXX: optimize this later. or don't, it's only called once. */
|
||||
|
||||
static void convert_placeholder(const uint8_t *rgb_in, int width, int height)
|
||||
{
|
||||
size_t size = width * height * 3;
|
||||
size_t linesize = width * 3;
|
||||
|
||||
std::vector<uint8_t> yuv_out;
|
||||
yuv_out.resize(size);
|
||||
|
||||
const uint8_t *in = rgb_in;
|
||||
const uint8_t *end = in + size;
|
||||
uint8_t *out = &yuv_out[0];
|
||||
|
||||
while (in < end) {
|
||||
const int16_t b = *(in++);
|
||||
const int16_t g = *(in++);
|
||||
const int16_t r = *(in++);
|
||||
|
||||
*(out++) = (uint8_t)(((66 * r + 129 * g + 25 * b + 128) >> 8) +
|
||||
16);
|
||||
*(out++) = (uint8_t)(((-38 * r - 74 * g + 112 * b + 128) >> 8) +
|
||||
128);
|
||||
*(out++) = (uint8_t)(((112 * r - 94 * g - 18 * b + 128) >> 8) +
|
||||
128);
|
||||
}
|
||||
|
||||
placeholder.resize(width * height * 3 / 2);
|
||||
|
||||
in = &yuv_out[0];
|
||||
end = in + size;
|
||||
|
||||
out = &placeholder[0];
|
||||
uint8_t *chroma = out + width * height;
|
||||
|
||||
while (in < end) {
|
||||
const uint8_t *in2 = in + linesize;
|
||||
const uint8_t *end2 = in2;
|
||||
uint8_t *out2 = out + width;
|
||||
|
||||
while (in < end2) {
|
||||
int16_t u;
|
||||
int16_t v;
|
||||
|
||||
*(out++) = *(in++);
|
||||
u = *(in++);
|
||||
v = *(in++);
|
||||
|
||||
*(out++) = *(in++);
|
||||
u += *(in++);
|
||||
v += *(in++);
|
||||
|
||||
*(out2++) = *(in2++);
|
||||
u += *(in2++);
|
||||
v += *(in2++);
|
||||
|
||||
*(out2++) = *(in2++);
|
||||
u += *(in2++);
|
||||
v += *(in2++);
|
||||
|
||||
*(chroma++) = (uint8_t)(u / 4);
|
||||
*(chroma++) = (uint8_t)(v / 4);
|
||||
}
|
||||
|
||||
in = in2;
|
||||
out = out2;
|
||||
}
|
||||
}
|
||||
|
||||
static bool load_placeholder_internal()
|
||||
{
|
||||
Status s;
|
||||
|
||||
wchar_t file[MAX_PATH];
|
||||
if (!GetModuleFileNameW(dll_inst, file, MAX_PATH)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
wchar_t *slash = wcsrchr(file, '\\');
|
||||
if (!slash) {
|
||||
return false;
|
||||
}
|
||||
|
||||
slash[1] = 0;
|
||||
|
||||
StringCbCat(file, sizeof(file), L"placeholder.png");
|
||||
|
||||
Bitmap bmp(file);
|
||||
if (bmp.GetLastStatus() != Status::Ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
BitmapData bmd = {};
|
||||
Rect r(0, 0, bmp.GetWidth(), bmp.GetHeight());
|
||||
|
||||
s = bmp.LockBits(&r, ImageLockModeRead, PixelFormat24bppRGB, &bmd);
|
||||
if (s != Status::Ok) {
|
||||
return false;
|
||||
}
|
||||
|
||||
convert_placeholder((const uint8_t *)bmd.Scan0, bmp.GetWidth(),
|
||||
bmp.GetHeight());
|
||||
|
||||
bmp.UnlockBits(&bmd);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool load_placeholder()
|
||||
{
|
||||
GdiplusStartupInput si;
|
||||
ULONG_PTR token;
|
||||
GdiplusStartup(&token, &si, nullptr);
|
||||
|
||||
bool success = load_placeholder_internal();
|
||||
|
||||
GdiplusShutdown(token);
|
||||
return success;
|
||||
}
|
||||
|
||||
const uint8_t *get_placeholder()
|
||||
{
|
||||
static bool failed = false;
|
||||
static bool initialized = false;
|
||||
|
||||
if (initialized) {
|
||||
return placeholder.data();
|
||||
} else if (failed) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
initialized = load_placeholder();
|
||||
failed = !initialized;
|
||||
|
||||
return initialized ? placeholder.data() : nullptr;
|
||||
}
|
50
plugins/win-dshow/virtualcam-module/sleepto.c
Normal file
50
plugins/win-dshow/virtualcam-module/sleepto.c
Normal file
@@ -0,0 +1,50 @@
|
||||
#include <windows.h>
|
||||
#include <stdbool.h>
|
||||
#include "sleepto.h"
|
||||
|
||||
static bool have_clockfreq = false;
|
||||
static LARGE_INTEGER clock_freq;
|
||||
|
||||
static inline uint64_t get_clockfreq(void)
|
||||
{
|
||||
if (!have_clockfreq) {
|
||||
QueryPerformanceFrequency(&clock_freq);
|
||||
have_clockfreq = true;
|
||||
}
|
||||
|
||||
return clock_freq.QuadPart;
|
||||
}
|
||||
|
||||
uint64_t gettime_100ns(void)
|
||||
{
|
||||
LARGE_INTEGER current_time;
|
||||
double time_val;
|
||||
|
||||
QueryPerformanceCounter(¤t_time);
|
||||
time_val = (double)current_time.QuadPart;
|
||||
time_val *= 10000000.0;
|
||||
time_val /= (double)get_clockfreq();
|
||||
|
||||
return (uint64_t)time_val;
|
||||
}
|
||||
|
||||
bool sleepto_100ns(uint64_t time_target)
|
||||
{
|
||||
uint64_t t = gettime_100ns();
|
||||
uint32_t milliseconds;
|
||||
|
||||
if (t >= time_target)
|
||||
return false;
|
||||
|
||||
milliseconds = (uint32_t)((time_target - t) / 10000);
|
||||
if (milliseconds > 1)
|
||||
Sleep(milliseconds - 1);
|
||||
|
||||
for (;;) {
|
||||
t = gettime_100ns();
|
||||
if (t >= time_target)
|
||||
return true;
|
||||
|
||||
Sleep(0);
|
||||
}
|
||||
}
|
14
plugins/win-dshow/virtualcam-module/sleepto.h
Normal file
14
plugins/win-dshow/virtualcam-module/sleepto.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
extern uint64_t gettime_100ns(void);
|
||||
extern bool sleepto_100ns(uint64_t time_target);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
241
plugins/win-dshow/virtualcam-module/virtualcam-filter.cpp
Normal file
241
plugins/win-dshow/virtualcam-module/virtualcam-filter.cpp
Normal file
@@ -0,0 +1,241 @@
|
||||
#include "virtualcam-filter.hpp"
|
||||
#include "sleepto.h"
|
||||
|
||||
#include <shlobj_core.h>
|
||||
#include <strsafe.h>
|
||||
#include <inttypes.h>
|
||||
|
||||
using namespace DShow;
|
||||
|
||||
extern const uint8_t *get_placeholder();
|
||||
|
||||
/* ========================================================================= */
|
||||
|
||||
VCamFilter::VCamFilter()
|
||||
: OutputFilter(VideoFormat::NV12, DEFAULT_CX, DEFAULT_CY,
|
||||
DEFAULT_INTERVAL)
|
||||
{
|
||||
thread_start = CreateEvent(nullptr, true, false, nullptr);
|
||||
thread_stop = CreateEvent(nullptr, true, false, nullptr);
|
||||
|
||||
AddVideoFormat(VideoFormat::I420, DEFAULT_CX, DEFAULT_CY,
|
||||
DEFAULT_INTERVAL);
|
||||
|
||||
/* ---------------------------------------- */
|
||||
/* load placeholder image */
|
||||
|
||||
placeholder = get_placeholder();
|
||||
|
||||
/* ---------------------------------------- */
|
||||
/* detect if this filter is within obs */
|
||||
|
||||
wchar_t file[MAX_PATH];
|
||||
if (!GetModuleFileNameW(nullptr, file, MAX_PATH)) {
|
||||
file[0] = 0;
|
||||
}
|
||||
|
||||
#ifdef _WIN64
|
||||
const wchar_t *obs_process = L"obs64.exe";
|
||||
#else
|
||||
const wchar_t *obs_process = L"obs32.exe";
|
||||
#endif
|
||||
|
||||
in_obs = !!wcsstr(file, obs_process);
|
||||
|
||||
/* ---------------------------------------- */
|
||||
/* add last/current obs res/interval */
|
||||
|
||||
uint32_t new_cx = cx;
|
||||
uint32_t new_cy = cy;
|
||||
uint64_t new_interval = interval;
|
||||
|
||||
vq = video_queue_open();
|
||||
if (vq) {
|
||||
if (video_queue_state(vq) == SHARED_QUEUE_STATE_READY) {
|
||||
video_queue_get_info(vq, &new_cx, &new_cy,
|
||||
&new_interval);
|
||||
}
|
||||
|
||||
/* don't keep it open until the filter actually starts */
|
||||
video_queue_close(vq);
|
||||
vq = nullptr;
|
||||
} else {
|
||||
wchar_t res_file[MAX_PATH];
|
||||
SHGetFolderPathW(nullptr, CSIDL_APPDATA, nullptr,
|
||||
SHGFP_TYPE_CURRENT, res_file);
|
||||
StringCbCat(res_file, sizeof(res_file),
|
||||
L"\\obs-virtualcam.txt");
|
||||
|
||||
HANDLE file = CreateFileW(res_file, GENERIC_READ, 0, nullptr,
|
||||
OPEN_EXISTING, 0, nullptr);
|
||||
if (file) {
|
||||
char res[128];
|
||||
DWORD len = 0;
|
||||
|
||||
ReadFile(file, res, sizeof(res), &len, nullptr);
|
||||
CloseHandle(file);
|
||||
|
||||
res[len] = 0;
|
||||
int vals = sscanf(res,
|
||||
"%" PRIu32 "x%" PRIu32 "x%" PRIu64,
|
||||
&new_cx, &new_cy, &new_interval);
|
||||
if (vals != 3) {
|
||||
new_cx = cx;
|
||||
new_cy = cy;
|
||||
new_interval = interval;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (new_cx != cx || new_cy != cy || new_interval != interval) {
|
||||
AddVideoFormat(VideoFormat::NV12, new_cx, new_cy, new_interval);
|
||||
AddVideoFormat(VideoFormat::I420, new_cx, new_cy, new_interval);
|
||||
SetVideoFormat(VideoFormat::NV12, new_cx, new_cy, new_interval);
|
||||
cx = new_cx;
|
||||
cy = new_cy;
|
||||
interval = new_interval;
|
||||
}
|
||||
|
||||
nv12_scale_init(&scaler, false, cx, cy, cx, cy);
|
||||
|
||||
/* ---------------------------------------- */
|
||||
|
||||
th = std::thread([this] { Thread(); });
|
||||
|
||||
AddRef();
|
||||
}
|
||||
|
||||
VCamFilter::~VCamFilter()
|
||||
{
|
||||
SetEvent(thread_stop);
|
||||
th.join();
|
||||
video_queue_close(vq);
|
||||
}
|
||||
|
||||
const wchar_t *VCamFilter::FilterName() const
|
||||
{
|
||||
return L"VCamFilter";
|
||||
}
|
||||
|
||||
STDMETHODIMP VCamFilter::Pause()
|
||||
{
|
||||
HRESULT hr;
|
||||
|
||||
hr = OutputFilter::Pause();
|
||||
if (FAILED(hr)) {
|
||||
return hr;
|
||||
}
|
||||
|
||||
SetEvent(thread_start);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
inline uint64_t VCamFilter::GetTime()
|
||||
{
|
||||
if (!!clock) {
|
||||
REFERENCE_TIME rt;
|
||||
HRESULT hr = clock->GetTime(&rt);
|
||||
if (SUCCEEDED(hr)) {
|
||||
return (uint64_t)rt;
|
||||
}
|
||||
}
|
||||
|
||||
return gettime_100ns();
|
||||
}
|
||||
|
||||
void VCamFilter::Thread()
|
||||
{
|
||||
HANDLE h[2] = {thread_start, thread_stop};
|
||||
DWORD ret = WaitForMultipleObjects(2, h, false, INFINITE);
|
||||
if (ret != WAIT_OBJECT_0)
|
||||
return;
|
||||
|
||||
uint64_t cur_time = gettime_100ns();
|
||||
uint64_t filter_time = GetTime();
|
||||
|
||||
cx = GetCX();
|
||||
cy = GetCY();
|
||||
interval = GetInterval();
|
||||
|
||||
nv12_scale_init(&scaler, false, GetCX(), GetCY(), cx, cy);
|
||||
|
||||
while (!stopped()) {
|
||||
Frame(filter_time);
|
||||
sleepto_100ns(cur_time += interval);
|
||||
filter_time += interval;
|
||||
}
|
||||
}
|
||||
|
||||
void VCamFilter::Frame(uint64_t ts)
|
||||
{
|
||||
uint32_t new_cx = cx;
|
||||
uint32_t new_cy = cy;
|
||||
uint64_t new_interval = interval;
|
||||
|
||||
if (!vq) {
|
||||
vq = video_queue_open();
|
||||
}
|
||||
|
||||
enum queue_state state = video_queue_state(vq);
|
||||
if (state != prev_state) {
|
||||
if (state == SHARED_QUEUE_STATE_READY) {
|
||||
video_queue_get_info(vq, &new_cx, &new_cy,
|
||||
&new_interval);
|
||||
} else if (state == SHARED_QUEUE_STATE_STOPPING) {
|
||||
video_queue_close(vq);
|
||||
vq = nullptr;
|
||||
}
|
||||
|
||||
prev_state = state;
|
||||
}
|
||||
|
||||
if (state != SHARED_QUEUE_STATE_READY) {
|
||||
new_cx = DEFAULT_CX;
|
||||
new_cy = DEFAULT_CY;
|
||||
new_interval = DEFAULT_INTERVAL;
|
||||
}
|
||||
|
||||
if (new_cx != cx || new_cy != cy || new_interval != interval) {
|
||||
if (in_obs) {
|
||||
SetVideoFormat(GetVideoFormat(), new_cx, new_cy,
|
||||
new_interval);
|
||||
}
|
||||
|
||||
nv12_scale_init(&scaler, false, GetCX(), GetCY(), new_cx,
|
||||
new_cy);
|
||||
|
||||
cx = new_cx;
|
||||
cy = new_cy;
|
||||
interval = new_interval;
|
||||
}
|
||||
|
||||
scaler.convert_to_i420 = GetVideoFormat() == VideoFormat::I420;
|
||||
|
||||
uint8_t *ptr;
|
||||
if (LockSampleData(&ptr)) {
|
||||
if (state == SHARED_QUEUE_STATE_READY)
|
||||
ShowOBSFrame(ptr);
|
||||
else
|
||||
ShowDefaultFrame(ptr);
|
||||
|
||||
UnlockSampleData(ts, ts + interval);
|
||||
}
|
||||
}
|
||||
|
||||
void VCamFilter::ShowOBSFrame(uint8_t *ptr)
|
||||
{
|
||||
uint64_t temp;
|
||||
if (!video_queue_read(vq, &scaler, ptr, &temp)) {
|
||||
video_queue_close(vq);
|
||||
vq = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void VCamFilter::ShowDefaultFrame(uint8_t *ptr)
|
||||
{
|
||||
if (placeholder) {
|
||||
nv12_do_scale(&scaler, ptr, placeholder);
|
||||
} else {
|
||||
memset(ptr, 127, GetCX() * GetCY() * 3 / 2);
|
||||
}
|
||||
}
|
51
plugins/win-dshow/virtualcam-module/virtualcam-filter.hpp
Normal file
51
plugins/win-dshow/virtualcam-module/virtualcam-filter.hpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#pragma once
|
||||
|
||||
#include <windows.h>
|
||||
#include <cstdint>
|
||||
#include <thread>
|
||||
#include "../shared-memory-queue.h"
|
||||
#include "../tiny-nv12-scale.h"
|
||||
#include "../libdshowcapture/source/output-filter.hpp"
|
||||
#include "../../../libobs/util/windows/WinHandle.hpp"
|
||||
|
||||
#define DEFAULT_CX 1920
|
||||
#define DEFAULT_CY 1080
|
||||
#define DEFAULT_INTERVAL 333333ULL
|
||||
|
||||
class VCamFilter : public DShow::OutputFilter {
|
||||
std::thread th;
|
||||
|
||||
video_queue_t *vq = nullptr;
|
||||
int queue_mode = 0;
|
||||
bool in_obs = false;
|
||||
enum queue_state prev_state = SHARED_QUEUE_STATE_INVALID;
|
||||
const uint8_t *placeholder;
|
||||
uint32_t cx = DEFAULT_CX;
|
||||
uint32_t cy = DEFAULT_CY;
|
||||
uint64_t interval = DEFAULT_INTERVAL;
|
||||
WinHandle thread_start;
|
||||
WinHandle thread_stop;
|
||||
|
||||
nv12_scale_t scaler = {};
|
||||
|
||||
inline bool stopped() const
|
||||
{
|
||||
return WaitForSingleObject(thread_stop, 0) != WAIT_TIMEOUT;
|
||||
}
|
||||
|
||||
inline uint64_t GetTime();
|
||||
|
||||
void Thread();
|
||||
void Frame(uint64_t ts);
|
||||
void ShowOBSFrame(uint8_t *ptr);
|
||||
void ShowDefaultFrame(uint8_t *ptr);
|
||||
|
||||
protected:
|
||||
const wchar_t *FilterName() const override;
|
||||
|
||||
public:
|
||||
VCamFilter();
|
||||
~VCamFilter() override;
|
||||
|
||||
STDMETHODIMP Pause() override;
|
||||
};
|
298
plugins/win-dshow/virtualcam-module/virtualcam-module.cpp
Normal file
298
plugins/win-dshow/virtualcam-module/virtualcam-module.cpp
Normal file
@@ -0,0 +1,298 @@
|
||||
#include "virtualcam-filter.hpp"
|
||||
#include "../virtualcam-guid.h"
|
||||
|
||||
/* ========================================================================= */
|
||||
|
||||
static const REGPINTYPES AMSMediaTypesV = {&MEDIATYPE_Video,
|
||||
&MEDIASUBTYPE_NV12};
|
||||
|
||||
static const REGFILTERPINS AMSPinVideo = {L"Output", false, true,
|
||||
false, false, &CLSID_NULL,
|
||||
nullptr, 1, &AMSMediaTypesV};
|
||||
|
||||
HINSTANCE dll_inst = nullptr;
|
||||
static volatile long locks = 0;
|
||||
|
||||
/* ========================================================================= */
|
||||
|
||||
class VCamFactory : public IClassFactory {
|
||||
volatile long refs = 1;
|
||||
CLSID cls;
|
||||
|
||||
public:
|
||||
inline VCamFactory(CLSID cls_) : cls(cls_) {}
|
||||
|
||||
// IUnknown
|
||||
STDMETHODIMP QueryInterface(REFIID riid, void **p_ptr);
|
||||
STDMETHODIMP_(ULONG) AddRef();
|
||||
STDMETHODIMP_(ULONG) Release();
|
||||
|
||||
// IClassFactory
|
||||
STDMETHODIMP CreateInstance(LPUNKNOWN parent, REFIID riid,
|
||||
void **p_ptr);
|
||||
STDMETHODIMP LockServer(BOOL lock);
|
||||
};
|
||||
|
||||
STDMETHODIMP VCamFactory::QueryInterface(REFIID riid, void **p_ptr)
|
||||
{
|
||||
if (!p_ptr) {
|
||||
return E_POINTER;
|
||||
}
|
||||
|
||||
if ((riid == IID_IUnknown) || (riid == IID_IClassFactory)) {
|
||||
AddRef();
|
||||
*p_ptr = (void *)this;
|
||||
return S_OK;
|
||||
} else {
|
||||
*p_ptr = nullptr;
|
||||
return E_NOINTERFACE;
|
||||
}
|
||||
}
|
||||
|
||||
STDMETHODIMP_(ULONG) VCamFactory::AddRef()
|
||||
{
|
||||
return InterlockedIncrement(&refs);
|
||||
}
|
||||
|
||||
STDMETHODIMP_(ULONG) VCamFactory::Release()
|
||||
{
|
||||
long new_refs = InterlockedDecrement(&refs);
|
||||
if (new_refs == 0) {
|
||||
delete this;
|
||||
return 0;
|
||||
}
|
||||
|
||||
return (ULONG)new_refs;
|
||||
}
|
||||
|
||||
STDMETHODIMP VCamFactory::CreateInstance(LPUNKNOWN parent, REFIID, void **p_ptr)
|
||||
{
|
||||
if (!p_ptr) {
|
||||
return E_POINTER;
|
||||
}
|
||||
|
||||
*p_ptr = nullptr;
|
||||
|
||||
/* don't bother supporting the "parent" functionality */
|
||||
if (parent) {
|
||||
return E_NOINTERFACE;
|
||||
}
|
||||
|
||||
if (IsEqualCLSID(cls, CLSID_OBS_VirtualVideo)) {
|
||||
*p_ptr = (void *)new VCamFilter();
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
return E_NOINTERFACE;
|
||||
}
|
||||
|
||||
STDMETHODIMP VCamFactory::LockServer(BOOL lock)
|
||||
{
|
||||
if (lock) {
|
||||
InterlockedIncrement(&locks);
|
||||
} else {
|
||||
InterlockedDecrement(&locks);
|
||||
}
|
||||
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
/* ========================================================================= */
|
||||
|
||||
static inline DWORD string_size(const wchar_t *str)
|
||||
{
|
||||
return (DWORD)(wcslen(str) + 1) * sizeof(wchar_t);
|
||||
}
|
||||
|
||||
static bool RegServer(CLSID cls, const wchar_t *desc, const wchar_t *file,
|
||||
const wchar_t *model = L"Both",
|
||||
const wchar_t *type = L"InprocServer32")
|
||||
{
|
||||
wchar_t cls_str[CHARS_IN_GUID];
|
||||
wchar_t temp[MAX_PATH];
|
||||
HKEY key = nullptr;
|
||||
HKEY subkey = nullptr;
|
||||
bool success = false;
|
||||
|
||||
StringFromGUID2(cls, cls_str, CHARS_IN_GUID);
|
||||
|
||||
StringCbPrintf(temp, sizeof(temp), L"CLSID\\%s", cls_str);
|
||||
|
||||
if (RegCreateKey(HKEY_CLASSES_ROOT, temp, &key) != ERROR_SUCCESS) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
RegSetValueW(key, nullptr, REG_SZ, desc, string_size(desc));
|
||||
|
||||
if (RegCreateKey(key, type, &subkey) != ERROR_SUCCESS) {
|
||||
goto fail;
|
||||
}
|
||||
|
||||
RegSetValueW(subkey, nullptr, REG_SZ, file, string_size(file));
|
||||
RegSetValueExW(subkey, L"ThreadingModel", 0, REG_SZ,
|
||||
(const BYTE *)model, string_size(model));
|
||||
|
||||
success = true;
|
||||
|
||||
fail:
|
||||
if (key) {
|
||||
RegCloseKey(key);
|
||||
}
|
||||
if (key) {
|
||||
RegCloseKey(subkey);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
static bool UnregServer(CLSID cls)
|
||||
{
|
||||
wchar_t cls_str[CHARS_IN_GUID];
|
||||
wchar_t temp[MAX_PATH];
|
||||
|
||||
StringFromGUID2(cls, cls_str, CHARS_IN_GUID);
|
||||
StringCbPrintf(temp, sizeof(temp), L"CLSID\\%s", cls_str);
|
||||
|
||||
return RegDeleteTreeW(HKEY_CLASSES_ROOT, temp) == ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
static bool RegServers(bool reg)
|
||||
{
|
||||
wchar_t file[MAX_PATH];
|
||||
|
||||
if (!GetModuleFileNameW(dll_inst, file, MAX_PATH)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (reg) {
|
||||
return RegServer(CLSID_OBS_VirtualVideo, L"OBS Virtual Camera",
|
||||
file);
|
||||
} else {
|
||||
return UnregServer(CLSID_OBS_VirtualVideo);
|
||||
}
|
||||
}
|
||||
|
||||
static bool RegFilters(bool reg)
|
||||
{
|
||||
ComPtr<IFilterMapper2> fm;
|
||||
HRESULT hr;
|
||||
|
||||
hr = CoCreateInstance(CLSID_FilterMapper2, nullptr,
|
||||
CLSCTX_INPROC_SERVER, IID_IFilterMapper2,
|
||||
(void **)&fm);
|
||||
if (FAILED(hr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (reg) {
|
||||
ComPtr<IMoniker> moniker;
|
||||
REGFILTER2 rf2;
|
||||
rf2.dwVersion = 1;
|
||||
rf2.dwMerit = MERIT_DO_NOT_USE;
|
||||
rf2.cPins = 1;
|
||||
rf2.rgPins = &AMSPinVideo;
|
||||
|
||||
hr = fm->RegisterFilter(CLSID_OBS_VirtualVideo,
|
||||
L"OBS Video Output", &moniker,
|
||||
&CLSID_VideoInputDeviceCategory,
|
||||
nullptr, &rf2);
|
||||
if (FAILED(hr)) {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
hr = fm->UnregisterFilter(&CLSID_VideoInputDeviceCategory, 0,
|
||||
CLSID_OBS_VirtualVideo);
|
||||
if (FAILED(hr)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/* ========================================================================= */
|
||||
|
||||
STDAPI DllRegisterServer()
|
||||
{
|
||||
if (!RegServers(true)) {
|
||||
RegServers(false);
|
||||
return E_FAIL;
|
||||
}
|
||||
|
||||
CoInitialize(0);
|
||||
|
||||
if (!RegFilters(true)) {
|
||||
RegFilters(false);
|
||||
RegServers(false);
|
||||
CoUninitialize();
|
||||
return E_FAIL;
|
||||
}
|
||||
|
||||
CoUninitialize();
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
STDAPI DllUnregisterServer()
|
||||
{
|
||||
CoInitialize(0);
|
||||
RegFilters(false);
|
||||
RegServers(false);
|
||||
CoUninitialize();
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
STDAPI DllInstall(BOOL install, LPCWSTR)
|
||||
{
|
||||
if (!install) {
|
||||
return DllUnregisterServer();
|
||||
} else {
|
||||
return DllRegisterServer();
|
||||
}
|
||||
}
|
||||
|
||||
STDAPI DllCanUnloadNow()
|
||||
{
|
||||
return InterlockedOr(&locks, 0) == 0 ? S_OK : S_FALSE;
|
||||
}
|
||||
|
||||
STDAPI DllGetClassObject(REFCLSID cls, REFIID riid, void **p_ptr)
|
||||
{
|
||||
if (!p_ptr) {
|
||||
return E_POINTER;
|
||||
}
|
||||
|
||||
*p_ptr = nullptr;
|
||||
|
||||
if (riid != IID_IClassFactory && riid != IID_IUnknown) {
|
||||
return E_NOINTERFACE;
|
||||
}
|
||||
if (!IsEqualCLSID(cls, CLSID_OBS_VirtualVideo)) {
|
||||
return E_INVALIDARG;
|
||||
}
|
||||
|
||||
*p_ptr = (void *)new VCamFactory(cls);
|
||||
return S_OK;
|
||||
}
|
||||
|
||||
//#define ENABLE_LOGGING
|
||||
|
||||
#ifdef ENABLE_LOGGING
|
||||
void logcallback(DShow::LogType, const wchar_t *msg, void *)
|
||||
{
|
||||
OutputDebugStringW(msg);
|
||||
OutputDebugStringW(L"\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, LPVOID)
|
||||
{
|
||||
if (reason == DLL_PROCESS_ATTACH) {
|
||||
DisableThreadLibraryCalls(inst);
|
||||
#ifdef ENABLE_LOGGING
|
||||
DShow::SetLogCallback(logcallback, nullptr);
|
||||
#endif
|
||||
dll_inst = inst;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
LIBRARY obs-virtualcam-module@_output_suffix@.dll
|
||||
EXPORTS
|
||||
DllMain PRIVATE
|
||||
DllGetClassObject PRIVATE
|
||||
DllCanUnloadNow PRIVATE
|
||||
DllRegisterServer PRIVATE
|
||||
DllUnregisterServer PRIVATE
|
||||
DllInstall PRIVATE
|
Reference in New Issue
Block a user