virtualcam-filter.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. /* ========================================================================= */
  11. VCamFilter::VCamFilter()
  12. : OutputFilter(VideoFormat::NV12, DEFAULT_CX, DEFAULT_CY,
  13. DEFAULT_INTERVAL)
  14. {
  15. thread_start = CreateEvent(nullptr, true, false, nullptr);
  16. thread_stop = CreateEvent(nullptr, true, false, nullptr);
  17. AddVideoFormat(VideoFormat::I420, DEFAULT_CX, DEFAULT_CY,
  18. DEFAULT_INTERVAL);
  19. AddVideoFormat(VideoFormat::YUY2, DEFAULT_CX, DEFAULT_CY,
  20. DEFAULT_INTERVAL);
  21. /* ---------------------------------------- */
  22. /* load placeholder image */
  23. if (initialize_placeholder()) {
  24. placeholder.data = get_placeholder_ptr();
  25. get_placeholder_size(&placeholder.cx, &placeholder.cy);
  26. } else {
  27. placeholder.data = nullptr;
  28. }
  29. /* ---------------------------------------- */
  30. /* detect if this filter is within obs */
  31. wchar_t file[MAX_PATH];
  32. if (!GetModuleFileNameW(nullptr, file, MAX_PATH)) {
  33. file[0] = 0;
  34. }
  35. #ifdef _WIN64
  36. const wchar_t *obs_process = L"obs64.exe";
  37. #else
  38. const wchar_t *obs_process = L"obs32.exe";
  39. #endif
  40. in_obs = !!wcsstr(file, obs_process);
  41. /* ---------------------------------------- */
  42. /* add last/current obs res/interval */
  43. uint32_t new_cx = cx;
  44. uint32_t new_cy = cy;
  45. uint64_t new_interval = interval;
  46. vq = video_queue_open();
  47. if (vq) {
  48. if (video_queue_state(vq) == SHARED_QUEUE_STATE_READY) {
  49. video_queue_get_info(vq, &new_cx, &new_cy,
  50. &new_interval);
  51. }
  52. /* don't keep it open until the filter actually starts */
  53. video_queue_close(vq);
  54. vq = nullptr;
  55. } else {
  56. wchar_t res_file[MAX_PATH];
  57. SHGetFolderPathW(nullptr, CSIDL_APPDATA, nullptr,
  58. SHGFP_TYPE_CURRENT, res_file);
  59. StringCbCat(res_file, sizeof(res_file),
  60. L"\\obs-virtualcam.txt");
  61. HANDLE file = CreateFileW(res_file, GENERIC_READ, 0, nullptr,
  62. OPEN_EXISTING, 0, nullptr);
  63. if (file) {
  64. char res[128];
  65. DWORD len = 0;
  66. if (ReadFile(file, res, sizeof(res) - 1, &len,
  67. nullptr)) {
  68. res[len] = 0;
  69. int vals = sscanf(
  70. res, "%" PRIu32 "x%" PRIu32 "x%" PRIu64,
  71. &new_cx, &new_cy, &new_interval);
  72. if (vals != 3) {
  73. new_cx = cx;
  74. new_cy = cy;
  75. new_interval = interval;
  76. }
  77. }
  78. CloseHandle(file);
  79. }
  80. }
  81. if (new_cx != cx || new_cy != cy || new_interval != interval) {
  82. AddVideoFormat(VideoFormat::NV12, new_cx, new_cy, new_interval);
  83. AddVideoFormat(VideoFormat::I420, new_cx, new_cy, new_interval);
  84. AddVideoFormat(VideoFormat::YUY2, new_cx, new_cy, new_interval);
  85. SetVideoFormat(VideoFormat::NV12, new_cx, new_cy, new_interval);
  86. cx = new_cx;
  87. cy = new_cy;
  88. interval = new_interval;
  89. }
  90. nv12_scale_init(&scaler, TARGET_FORMAT_NV12, cx, cy, cx, cy);
  91. if (placeholder.data)
  92. nv12_scale_init(&placeholder.scaler, TARGET_FORMAT_NV12,
  93. GetCX(), GetCY(), placeholder.cx,
  94. placeholder.cy);
  95. /* ---------------------------------------- */
  96. th = std::thread([this] { Thread(); });
  97. AddRef();
  98. }
  99. VCamFilter::~VCamFilter()
  100. {
  101. SetEvent(thread_stop);
  102. th.join();
  103. video_queue_close(vq);
  104. }
  105. const wchar_t *VCamFilter::FilterName() const
  106. {
  107. return L"VCamFilter";
  108. }
  109. STDMETHODIMP VCamFilter::Pause()
  110. {
  111. HRESULT hr;
  112. hr = OutputFilter::Pause();
  113. if (FAILED(hr)) {
  114. return hr;
  115. }
  116. SetEvent(thread_start);
  117. return S_OK;
  118. }
  119. inline uint64_t VCamFilter::GetTime()
  120. {
  121. if (!!clock) {
  122. REFERENCE_TIME rt;
  123. HRESULT hr = clock->GetTime(&rt);
  124. if (SUCCEEDED(hr)) {
  125. return (uint64_t)rt;
  126. }
  127. }
  128. return gettime_100ns();
  129. }
  130. void VCamFilter::Thread()
  131. {
  132. HANDLE h[2] = {thread_start, thread_stop};
  133. DWORD ret = WaitForMultipleObjects(2, h, false, INFINITE);
  134. if (ret != WAIT_OBJECT_0)
  135. return;
  136. uint64_t cur_time = gettime_100ns();
  137. uint64_t filter_time = GetTime();
  138. cx = GetCX();
  139. cy = GetCY();
  140. interval = GetInterval();
  141. nv12_scale_init(&scaler, TARGET_FORMAT_NV12, GetCX(), GetCY(), cx, cy);
  142. if (placeholder.data)
  143. nv12_scale_init(&placeholder.scaler, TARGET_FORMAT_NV12,
  144. GetCX(), GetCY(), placeholder.cx,
  145. placeholder.cy);
  146. while (!stopped()) {
  147. Frame(filter_time);
  148. sleepto_100ns(cur_time += interval);
  149. filter_time += interval;
  150. }
  151. }
  152. void VCamFilter::Frame(uint64_t ts)
  153. {
  154. uint32_t new_cx = cx;
  155. uint32_t new_cy = cy;
  156. uint64_t new_interval = interval;
  157. if (!vq) {
  158. vq = video_queue_open();
  159. }
  160. enum queue_state state = video_queue_state(vq);
  161. if (state != prev_state) {
  162. if (state == SHARED_QUEUE_STATE_READY) {
  163. video_queue_get_info(vq, &new_cx, &new_cy,
  164. &new_interval);
  165. } else if (state == SHARED_QUEUE_STATE_STOPPING) {
  166. video_queue_close(vq);
  167. vq = nullptr;
  168. }
  169. prev_state = state;
  170. }
  171. if (state != SHARED_QUEUE_STATE_READY) {
  172. new_cx = GetCX();
  173. new_cy = GetCY();
  174. new_interval = GetInterval();
  175. }
  176. if (new_cx != cx || new_cy != cy || new_interval != interval) {
  177. if (in_obs) {
  178. SetVideoFormat(GetVideoFormat(), new_cx, new_cy,
  179. new_interval);
  180. }
  181. nv12_scale_init(&scaler, TARGET_FORMAT_NV12, GetCX(), GetCY(),
  182. new_cx, new_cy);
  183. if (placeholder.data)
  184. nv12_scale_init(&placeholder.scaler, TARGET_FORMAT_NV12,
  185. GetCX(), GetCY(), placeholder.cx,
  186. placeholder.cy);
  187. cx = new_cx;
  188. cy = new_cy;
  189. interval = new_interval;
  190. }
  191. if (GetVideoFormat() == VideoFormat::I420)
  192. scaler.format = placeholder.scaler.format = TARGET_FORMAT_I420;
  193. else if (GetVideoFormat() == VideoFormat::YUY2)
  194. scaler.format = placeholder.scaler.format = TARGET_FORMAT_YUY2;
  195. else
  196. scaler.format = placeholder.scaler.format = TARGET_FORMAT_NV12;
  197. uint8_t *ptr;
  198. if (LockSampleData(&ptr)) {
  199. if (state == SHARED_QUEUE_STATE_READY)
  200. ShowOBSFrame(ptr);
  201. else
  202. ShowDefaultFrame(ptr);
  203. UnlockSampleData(ts, ts + interval);
  204. }
  205. }
  206. void VCamFilter::ShowOBSFrame(uint8_t *ptr)
  207. {
  208. uint64_t temp;
  209. if (!video_queue_read(vq, &scaler, ptr, &temp)) {
  210. video_queue_close(vq);
  211. vq = nullptr;
  212. }
  213. }
  214. void VCamFilter::ShowDefaultFrame(uint8_t *ptr)
  215. {
  216. if (placeholder.data) {
  217. nv12_do_scale(&placeholder.scaler, ptr, placeholder.data);
  218. } else {
  219. memset(ptr, 127, GetCX() * GetCY() * 3 / 2);
  220. }
  221. }