virtualcam-filter.cpp 9.3 KB

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