virtualcam-filter.cpp 5.4 KB

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