virtualcam-filter.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. #include "virtualcam-filter.hpp"
  2. #include "sleepto.h"
  3. #include <shlobj_core.h>
  4. #include <strsafe.h>
  5. #include <inttypes.h>
  6. using namespace DShow;
  7. extern bool initialize_placeholder();
  8. extern const uint8_t *get_placeholder_ptr();
  9. extern const bool get_placeholder_size(int *out_cx, int *out_cy);
  10. extern volatile long locks;
  11. /* ========================================================================= */
  12. VCamFilter::VCamFilter()
  13. : OutputFilter(VideoFormat::NV12, DEFAULT_CX, DEFAULT_CY,
  14. DEFAULT_INTERVAL)
  15. {
  16. thread_start = CreateEvent(nullptr, true, false, nullptr);
  17. thread_stop = CreateEvent(nullptr, true, false, nullptr);
  18. AddVideoFormat(VideoFormat::I420, DEFAULT_CX, DEFAULT_CY,
  19. DEFAULT_INTERVAL);
  20. AddVideoFormat(VideoFormat::YUY2, DEFAULT_CX, DEFAULT_CY,
  21. DEFAULT_INTERVAL);
  22. format = VideoFormat::NV12;
  23. placeholder.scaled_data = nullptr;
  24. /* ---------------------------------------- */
  25. /* detect if this filter is within obs */
  26. wchar_t file[MAX_PATH];
  27. if (!GetModuleFileNameW(nullptr, file, MAX_PATH)) {
  28. file[0] = 0;
  29. }
  30. #ifdef _WIN64
  31. const wchar_t *obs_process = L"obs64.exe";
  32. #else
  33. const wchar_t *obs_process = L"obs32.exe";
  34. #endif
  35. in_obs = !!wcsstr(file, obs_process);
  36. /* ---------------------------------------- */
  37. /* add last/current obs res/interval */
  38. uint32_t new_obs_cx = obs_cx;
  39. uint32_t new_obs_cy = obs_cy;
  40. uint64_t new_obs_interval = obs_interval;
  41. vq = video_queue_open();
  42. if (vq) {
  43. if (video_queue_state(vq) == SHARED_QUEUE_STATE_READY) {
  44. video_queue_get_info(vq, &new_obs_cx, &new_obs_cy,
  45. &new_obs_interval);
  46. }
  47. /* don't keep it open until the filter actually starts */
  48. video_queue_close(vq);
  49. vq = nullptr;
  50. } else {
  51. wchar_t res_file[MAX_PATH];
  52. SHGetFolderPathW(nullptr, CSIDL_APPDATA, nullptr,
  53. SHGFP_TYPE_CURRENT, res_file);
  54. StringCbCat(res_file, sizeof(res_file),
  55. L"\\obs-virtualcam.txt");
  56. HANDLE file = CreateFileW(res_file, GENERIC_READ, 0, nullptr,
  57. OPEN_EXISTING, 0, nullptr);
  58. if (file) {
  59. char res[128];
  60. DWORD len = 0;
  61. if (ReadFile(file, res, sizeof(res) - 1, &len,
  62. nullptr)) {
  63. res[len] = 0;
  64. int vals = sscanf(res,
  65. "%" PRIu32 "x%" PRIu32
  66. "x%" PRIu64,
  67. &new_obs_cx, &new_obs_cy,
  68. &new_obs_interval);
  69. if (vals != 3) {
  70. new_obs_cx = obs_cx;
  71. new_obs_cy = obs_cy;
  72. new_obs_interval = obs_interval;
  73. }
  74. }
  75. CloseHandle(file);
  76. }
  77. }
  78. if (new_obs_cx != obs_cx || new_obs_cy != obs_cy ||
  79. new_obs_interval != obs_interval) {
  80. AddVideoFormat(VideoFormat::NV12, new_obs_cx, new_obs_cy,
  81. new_obs_interval);
  82. AddVideoFormat(VideoFormat::I420, new_obs_cx, new_obs_cy,
  83. new_obs_interval);
  84. AddVideoFormat(VideoFormat::YUY2, new_obs_cx, new_obs_cy,
  85. new_obs_interval);
  86. SetVideoFormat(VideoFormat::NV12, new_obs_cx, new_obs_cy,
  87. new_obs_interval);
  88. obs_cx = new_obs_cx;
  89. obs_cy = new_obs_cy;
  90. obs_interval = new_obs_interval;
  91. }
  92. /* ---------------------------------------- */
  93. th = std::thread([this] { Thread(); });
  94. AddRef();
  95. os_atomic_inc_long(&locks);
  96. }
  97. VCamFilter::~VCamFilter()
  98. {
  99. SetEvent(thread_stop);
  100. if (th.joinable())
  101. th.join();
  102. video_queue_close(vq);
  103. if (placeholder.scaled_data)
  104. free(placeholder.scaled_data);
  105. os_atomic_dec_long(&locks);
  106. }
  107. const wchar_t *VCamFilter::FilterName() const
  108. {
  109. return L"VCamFilter";
  110. }
  111. STDMETHODIMP VCamFilter::Pause()
  112. {
  113. HRESULT hr;
  114. hr = OutputFilter::Pause();
  115. if (FAILED(hr)) {
  116. return hr;
  117. }
  118. os_atomic_set_bool(&active, true);
  119. SetEvent(thread_start);
  120. return S_OK;
  121. }
  122. STDMETHODIMP VCamFilter::Run(REFERENCE_TIME tStart)
  123. {
  124. os_atomic_set_bool(&active, true);
  125. return OutputFilter::Run(tStart);
  126. }
  127. STDMETHODIMP VCamFilter::Stop()
  128. {
  129. os_atomic_set_bool(&active, false);
  130. return OutputFilter::Stop();
  131. }
  132. inline uint64_t VCamFilter::GetTime()
  133. {
  134. if (!!clock) {
  135. REFERENCE_TIME rt;
  136. HRESULT hr = clock->GetTime(&rt);
  137. if (SUCCEEDED(hr)) {
  138. return (uint64_t)rt;
  139. }
  140. }
  141. return gettime_100ns();
  142. }
  143. void VCamFilter::Thread()
  144. {
  145. HANDLE h[2] = {thread_start, thread_stop};
  146. DWORD ret = WaitForMultipleObjects(2, h, false, INFINITE);
  147. if (ret != WAIT_OBJECT_0)
  148. return;
  149. uint64_t cur_time = gettime_100ns();
  150. uint64_t filter_time = GetTime();
  151. obs_cx = (uint32_t)GetCX();
  152. obs_cy = (uint32_t)GetCY();
  153. obs_interval = (uint64_t)GetInterval();
  154. filter_cx = obs_cx;
  155. filter_cy = obs_cy;
  156. /* ---------------------------------------- */
  157. /* load placeholder image */
  158. if (initialize_placeholder()) {
  159. placeholder.source_data = get_placeholder_ptr();
  160. get_placeholder_size(&placeholder.cx, &placeholder.cy);
  161. } else {
  162. placeholder.source_data = nullptr;
  163. }
  164. /* Created dynamically based on output resolution changes */
  165. placeholder.scaled_data = nullptr;
  166. nv12_scale_init(&scaler, TARGET_FORMAT_NV12, obs_cx, obs_cy, obs_cx,
  167. obs_cy);
  168. nv12_scale_init(&placeholder.scaler, TARGET_FORMAT_NV12, obs_cx, obs_cy,
  169. placeholder.cx, placeholder.cy);
  170. UpdatePlaceholder();
  171. while (!stopped()) {
  172. if (os_atomic_load_bool(&active))
  173. Frame(filter_time);
  174. sleepto_100ns(cur_time += obs_interval);
  175. filter_time += obs_interval;
  176. }
  177. }
  178. void VCamFilter::Frame(uint64_t ts)
  179. {
  180. uint32_t new_obs_cx = obs_cx;
  181. uint32_t new_obs_cy = obs_cy;
  182. uint64_t new_obs_interval = obs_interval;
  183. /* cx, cy and interval are the resolution and frame rate of the
  184. virtual camera _source_, ie OBS' output. Do not confuse cx / cy
  185. with GetCX() / GetCY() / GetInterval() which return the virtualcam
  186. filter output! */
  187. if (!vq) {
  188. vq = video_queue_open();
  189. }
  190. enum queue_state state = video_queue_state(vq);
  191. if (state != prev_state) {
  192. if (state == SHARED_QUEUE_STATE_READY) {
  193. /* The virtualcam output from OBS has started, get
  194. the actual cx / cy of the data stream */
  195. video_queue_get_info(vq, &new_obs_cx, &new_obs_cy,
  196. &new_obs_interval);
  197. } else if (state == SHARED_QUEUE_STATE_STOPPING) {
  198. video_queue_close(vq);
  199. vq = nullptr;
  200. }
  201. prev_state = state;
  202. }
  203. uint32_t new_filter_cx = (uint32_t)GetCX();
  204. uint32_t new_filter_cy = (uint32_t)GetCY();
  205. if (state != SHARED_QUEUE_STATE_READY) {
  206. /* Virtualcam output not yet started, assume it's
  207. the same resolution as the filter output */
  208. new_obs_cx = new_filter_cx;
  209. new_obs_cy = new_filter_cy;
  210. new_obs_interval = GetInterval();
  211. }
  212. if (new_obs_cx != obs_cx || new_obs_cy != obs_cy ||
  213. new_obs_interval != obs_interval) {
  214. /* The res / FPS of the video coming from OBS has
  215. changed, update parameters as needed */
  216. if (in_obs) {
  217. /* If the vcam is being used inside obs, adjust
  218. the format we present to match */
  219. SetVideoFormat(GetVideoFormat(), new_obs_cx, new_obs_cy,
  220. new_obs_interval);
  221. }
  222. /* Re-initialize the main scaler to use the new resolution */
  223. nv12_scale_init(&scaler, scaler.format, new_filter_cx,
  224. new_filter_cy, new_obs_cx, new_obs_cy);
  225. obs_cx = new_obs_cx;
  226. obs_cy = new_obs_cy;
  227. obs_interval = new_obs_interval;
  228. filter_cx = new_filter_cx;
  229. filter_cy = new_filter_cy;
  230. UpdatePlaceholder();
  231. } else if (new_filter_cx != filter_cx || new_filter_cy != filter_cy) {
  232. filter_cx = new_filter_cx;
  233. filter_cy = new_filter_cy;
  234. /* Re-initialize the main scaler to use the new resolution */
  235. nv12_scale_init(&scaler, scaler.format, new_filter_cx,
  236. new_filter_cy, new_obs_cx, new_obs_cy);
  237. UpdatePlaceholder();
  238. }
  239. VideoFormat current_format = GetVideoFormat();
  240. if (current_format != format) {
  241. /* The output format changed, update the scalers */
  242. if (current_format == VideoFormat::I420)
  243. scaler.format = placeholder.scaler.format =
  244. TARGET_FORMAT_I420;
  245. else if (current_format == VideoFormat::YUY2)
  246. scaler.format = placeholder.scaler.format =
  247. TARGET_FORMAT_YUY2;
  248. else
  249. scaler.format = placeholder.scaler.format =
  250. TARGET_FORMAT_NV12;
  251. format = current_format;
  252. UpdatePlaceholder();
  253. }
  254. /* Actual output */
  255. uint8_t *ptr;
  256. if (LockSampleData(&ptr)) {
  257. if (state == SHARED_QUEUE_STATE_READY)
  258. ShowOBSFrame(ptr);
  259. else
  260. ShowDefaultFrame(ptr);
  261. UnlockSampleData(ts, ts + obs_interval);
  262. }
  263. }
  264. void VCamFilter::ShowOBSFrame(uint8_t *ptr)
  265. {
  266. uint64_t temp;
  267. if (!video_queue_read(vq, &scaler, ptr, &temp)) {
  268. video_queue_close(vq);
  269. vq = nullptr;
  270. }
  271. }
  272. void VCamFilter::ShowDefaultFrame(uint8_t *ptr)
  273. {
  274. if (placeholder.scaled_data) {
  275. memcpy(ptr, placeholder.scaled_data, GetOutputBufferSize());
  276. } else {
  277. memset(ptr, 127, GetOutputBufferSize());
  278. }
  279. }
  280. /* Called when the output resolution or format has changed to re-scale
  281. the placeholder graphic into the placeholder.scaled_data buffer. */
  282. void VCamFilter::UpdatePlaceholder(void)
  283. {
  284. if (!placeholder.source_data)
  285. return;
  286. if (placeholder.scaled_data)
  287. free(placeholder.scaled_data);
  288. placeholder.scaled_data = (uint8_t *)malloc(GetOutputBufferSize());
  289. if (!placeholder.scaled_data)
  290. return;
  291. if (placeholder.cx == GetCX() && placeholder.cy == GetCY() &&
  292. placeholder.scaler.format == TARGET_FORMAT_NV12) {
  293. /* No scaling necessary if it matches exactly */
  294. memcpy(placeholder.scaled_data, placeholder.source_data,
  295. GetOutputBufferSize());
  296. } else {
  297. nv12_scale_init(&placeholder.scaler, placeholder.scaler.format,
  298. GetCX(), GetCY(), placeholder.cx,
  299. placeholder.cy);
  300. nv12_do_scale(&placeholder.scaler, placeholder.scaled_data,
  301. placeholder.source_data);
  302. }
  303. }
  304. /* Calculate the size of the output buffer based on the filter's
  305. resolution and format */
  306. const int VCamFilter::GetOutputBufferSize(void)
  307. {
  308. int bits = VFormatBits(format);
  309. return GetCX() * GetCY() * bits / 8;
  310. }