virtualcam-filter.cpp 8.0 KB

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