virtualcam-filter.cpp 9.1 KB

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