virtualcam-filter.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. th.join();
  92. video_queue_close(vq);
  93. if (placeholder.scaled_data)
  94. free(placeholder.scaled_data);
  95. }
  96. const wchar_t *VCamFilter::FilterName() const
  97. {
  98. return L"VCamFilter";
  99. }
  100. STDMETHODIMP VCamFilter::Pause()
  101. {
  102. HRESULT hr;
  103. hr = OutputFilter::Pause();
  104. if (FAILED(hr)) {
  105. return hr;
  106. }
  107. SetEvent(thread_start);
  108. return S_OK;
  109. }
  110. inline uint64_t VCamFilter::GetTime()
  111. {
  112. if (!!clock) {
  113. REFERENCE_TIME rt;
  114. HRESULT hr = clock->GetTime(&rt);
  115. if (SUCCEEDED(hr)) {
  116. return (uint64_t)rt;
  117. }
  118. }
  119. return gettime_100ns();
  120. }
  121. void VCamFilter::Thread()
  122. {
  123. HANDLE h[2] = {thread_start, thread_stop};
  124. DWORD ret = WaitForMultipleObjects(2, h, false, INFINITE);
  125. if (ret != WAIT_OBJECT_0)
  126. return;
  127. uint64_t cur_time = gettime_100ns();
  128. uint64_t filter_time = GetTime();
  129. cx = GetCX();
  130. cy = GetCY();
  131. interval = GetInterval();
  132. /* ---------------------------------------- */
  133. /* load placeholder image */
  134. if (initialize_placeholder()) {
  135. placeholder.source_data = get_placeholder_ptr();
  136. get_placeholder_size(&placeholder.cx, &placeholder.cy);
  137. } else {
  138. placeholder.source_data = nullptr;
  139. }
  140. /* Created dynamically based on output resolution changes */
  141. placeholder.scaled_data = nullptr;
  142. nv12_scale_init(&scaler, TARGET_FORMAT_NV12, cx, cy, cx, cy);
  143. nv12_scale_init(&placeholder.scaler, TARGET_FORMAT_NV12, cx, cy,
  144. placeholder.cx, placeholder.cy);
  145. UpdatePlaceholder();
  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. /* cx, cy and interval are the resolution and frame rate of the
  158. virtual camera _source_, ie OBS' output. Do not confuse cx / cy
  159. with GetCX() / GetCY() / GetInterval() which return the virtualcam
  160. filter output! */
  161. if (!vq) {
  162. vq = video_queue_open();
  163. }
  164. enum queue_state state = video_queue_state(vq);
  165. if (state != prev_state) {
  166. if (state == SHARED_QUEUE_STATE_READY) {
  167. /* The virtualcam output from OBS has started, get
  168. the actual cx / cy of the data stream */
  169. video_queue_get_info(vq, &new_cx, &new_cy,
  170. &new_interval);
  171. } else if (state == SHARED_QUEUE_STATE_STOPPING) {
  172. video_queue_close(vq);
  173. vq = nullptr;
  174. }
  175. prev_state = state;
  176. }
  177. if (state != SHARED_QUEUE_STATE_READY) {
  178. /* Virtualcam output not yet started, assume it's
  179. the same resolution as the filter output */
  180. new_cx = GetCX();
  181. new_cy = GetCY();
  182. new_interval = GetInterval();
  183. }
  184. if (new_cx != cx || new_cy != cy || new_interval != interval) {
  185. /* The res / FPS of the video coming from OBS has
  186. changed, update parameters as needed */
  187. if (in_obs) {
  188. /* If the vcam is being used inside obs, adjust
  189. the format we present to match */
  190. SetVideoFormat(GetVideoFormat(), new_cx, new_cy,
  191. new_interval);
  192. }
  193. /* Re-initialize the main scaler to use the new resolution */
  194. nv12_scale_init(&scaler, scaler.format, GetCX(), GetCY(),
  195. new_cx, new_cy);
  196. cx = new_cx;
  197. cy = new_cy;
  198. interval = new_interval;
  199. UpdatePlaceholder();
  200. }
  201. VideoFormat current_format = GetVideoFormat();
  202. if (current_format != format) {
  203. /* The output format changed, update the scalers */
  204. if (current_format == VideoFormat::I420)
  205. scaler.format = placeholder.scaler.format =
  206. TARGET_FORMAT_I420;
  207. else if (current_format == VideoFormat::YUY2)
  208. scaler.format = placeholder.scaler.format =
  209. TARGET_FORMAT_YUY2;
  210. else
  211. scaler.format = placeholder.scaler.format =
  212. TARGET_FORMAT_NV12;
  213. format = current_format;
  214. UpdatePlaceholder();
  215. }
  216. /* Actual output */
  217. uint8_t *ptr;
  218. if (LockSampleData(&ptr)) {
  219. if (state == SHARED_QUEUE_STATE_READY)
  220. ShowOBSFrame(ptr);
  221. else
  222. ShowDefaultFrame(ptr);
  223. UnlockSampleData(ts, ts + interval);
  224. }
  225. }
  226. void VCamFilter::ShowOBSFrame(uint8_t *ptr)
  227. {
  228. uint64_t temp;
  229. if (!video_queue_read(vq, &scaler, ptr, &temp)) {
  230. video_queue_close(vq);
  231. vq = nullptr;
  232. }
  233. }
  234. void VCamFilter::ShowDefaultFrame(uint8_t *ptr)
  235. {
  236. if (placeholder.scaled_data) {
  237. memcpy(ptr, placeholder.scaled_data, GetOutputBufferSize());
  238. } else {
  239. memset(ptr, 127, GetOutputBufferSize());
  240. }
  241. }
  242. /* Called when the output resolution or format has changed to re-scale
  243. the placeholder graphic into the placeholder.scaled_data buffer. */
  244. void VCamFilter::UpdatePlaceholder(void)
  245. {
  246. if (!placeholder.source_data)
  247. return;
  248. if (placeholder.scaled_data)
  249. free(placeholder.scaled_data);
  250. placeholder.scaled_data = (uint8_t *)malloc(GetOutputBufferSize());
  251. if (!placeholder.scaled_data)
  252. return;
  253. if (placeholder.cx == GetCX() && placeholder.cy == GetCY() &&
  254. placeholder.scaler.format == TARGET_FORMAT_NV12) {
  255. /* No scaling necessary if it matches exactly */
  256. memcpy(placeholder.scaled_data, placeholder.source_data,
  257. GetOutputBufferSize());
  258. } else {
  259. nv12_scale_init(&placeholder.scaler, placeholder.scaler.format,
  260. GetCX(), GetCY(), placeholder.cx,
  261. placeholder.cy);
  262. nv12_do_scale(&placeholder.scaler, placeholder.scaled_data,
  263. placeholder.source_data);
  264. }
  265. }
  266. /* Calculate the size of the output buffer based on the filter's
  267. resolution and format */
  268. const int VCamFilter::GetOutputBufferSize(void)
  269. {
  270. int bits = VFormatBits(format);
  271. return GetCX() * GetCY() * bits / 8;
  272. }