win-dshow.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  1. #include <objbase.h>
  2. #include <obs-module.h>
  3. #include <obs.hpp>
  4. #include <util/dstr.hpp>
  5. #include <util/platform.h>
  6. #include <util/windows/WinHandle.hpp>
  7. #include <util/threading.h>
  8. #include "libdshowcapture/dshowcapture.hpp"
  9. #include "ffmpeg-decode.h"
  10. #include "encode-dstr.hpp"
  11. #include <algorithm>
  12. #include <limits>
  13. #include <set>
  14. #include <string>
  15. #include <vector>
  16. /*
  17. * TODO:
  18. * - handle disconnections and reconnections
  19. * - if device not present, wait for device to be plugged in
  20. */
  21. #undef min
  22. #undef max
  23. using namespace std;
  24. using namespace DShow;
  25. /* settings defines that will cause errors if there are typos */
  26. #define VIDEO_DEVICE_ID "video_device_id"
  27. #define RES_TYPE "res_type"
  28. #define RESOLUTION "resolution"
  29. #define FRAME_INTERVAL "frame_interval"
  30. #define VIDEO_FORMAT "video_format"
  31. #define LAST_VIDEO_DEV_ID "last_video_device_id"
  32. #define LAST_RESOLUTION "last_resolution"
  33. #define BUFFERING_VAL "buffering"
  34. #define FLIP_IMAGE "flip_vertically"
  35. #define AUDIO_OUTPUT_MODE "audio_output_mode"
  36. #define USE_CUSTOM_AUDIO "use_custom_audio_device"
  37. #define AUDIO_DEVICE_ID "audio_device_id"
  38. #define COLOR_SPACE "color_space"
  39. #define COLOR_RANGE "color_range"
  40. #define DEACTIVATE_WNS "deactivate_when_not_showing"
  41. #define TEXT_INPUT_NAME obs_module_text("VideoCaptureDevice")
  42. #define TEXT_DEVICE obs_module_text("Device")
  43. #define TEXT_CONFIG_VIDEO obs_module_text("ConfigureVideo")
  44. #define TEXT_CONFIG_XBAR obs_module_text("ConfigureCrossbar")
  45. #define TEXT_RES_FPS_TYPE obs_module_text("ResFPSType")
  46. #define TEXT_CUSTOM_RES obs_module_text("ResFPSType.Custom")
  47. #define TEXT_PREFERRED_RES obs_module_text("ResFPSType.DevPreferred")
  48. #define TEXT_FPS_MATCHING obs_module_text("FPS.Matching")
  49. #define TEXT_FPS_HIGHEST obs_module_text("FPS.Highest")
  50. #define TEXT_RESOLUTION obs_module_text("Resolution")
  51. #define TEXT_VIDEO_FORMAT obs_module_text("VideoFormat")
  52. #define TEXT_FORMAT_UNKNOWN obs_module_text("VideoFormat.Unknown")
  53. #define TEXT_BUFFERING obs_module_text("Buffering")
  54. #define TEXT_BUFFERING_AUTO obs_module_text("Buffering.AutoDetect")
  55. #define TEXT_BUFFERING_ON obs_module_text("Buffering.Enable")
  56. #define TEXT_BUFFERING_OFF obs_module_text("Buffering.Disable")
  57. #define TEXT_FLIP_IMAGE obs_module_text("FlipVertically")
  58. #define TEXT_AUDIO_MODE obs_module_text("AudioOutputMode")
  59. #define TEXT_MODE_CAPTURE obs_module_text("AudioOutputMode.Capture")
  60. #define TEXT_MODE_DSOUND obs_module_text("AudioOutputMode.DirectSound")
  61. #define TEXT_MODE_WAVEOUT obs_module_text("AudioOutputMode.WaveOut")
  62. #define TEXT_CUSTOM_AUDIO obs_module_text("UseCustomAudioDevice")
  63. #define TEXT_AUDIO_DEVICE obs_module_text("AudioDevice")
  64. #define TEXT_ACTIVATE obs_module_text("Activate")
  65. #define TEXT_DEACTIVATE obs_module_text("Deactivate")
  66. #define TEXT_COLOR_SPACE obs_module_text("ColorSpace")
  67. #define TEXT_COLOR_DEFAULT obs_module_text("ColorSpace.Default")
  68. #define TEXT_COLOR_RANGE obs_module_text("ColorRange")
  69. #define TEXT_RANGE_PARTIAL obs_module_text("ColorRange.Partial")
  70. #define TEXT_RANGE_FULL obs_module_text("ColorRange.Full")
  71. #define TEXT_DWNS obs_module_text("DeactivateWhenNotShowing")
  72. enum ResType {
  73. ResType_Preferred,
  74. ResType_Custom
  75. };
  76. enum class BufferingType : int64_t {
  77. Auto,
  78. On,
  79. Off
  80. };
  81. void ffmpeg_log(void *bla, int level, const char *msg, va_list args)
  82. {
  83. DStr str;
  84. if (level == AV_LOG_WARNING)
  85. dstr_copy(str, "warning: ");
  86. else if (level == AV_LOG_ERROR)
  87. dstr_copy(str, "error: ");
  88. else if (level < AV_LOG_ERROR)
  89. dstr_copy(str, "fatal: ");
  90. else
  91. return;
  92. dstr_cat(str, msg);
  93. if (dstr_end(str) == '\n')
  94. dstr_resize(str, str->len - 1);
  95. blogva(LOG_WARNING, str, args);
  96. av_log_default_callback(bla, level, msg, args);
  97. }
  98. class Decoder {
  99. struct ffmpeg_decode decode;
  100. public:
  101. inline Decoder() {memset(&decode, 0, sizeof(decode));}
  102. inline ~Decoder() {ffmpeg_decode_free(&decode);}
  103. inline operator ffmpeg_decode*() {return &decode;}
  104. inline ffmpeg_decode *operator->() {return &decode;}
  105. };
  106. class CriticalSection {
  107. CRITICAL_SECTION mutex;
  108. public:
  109. inline CriticalSection() {InitializeCriticalSection(&mutex);}
  110. inline ~CriticalSection() {DeleteCriticalSection(&mutex);}
  111. inline operator CRITICAL_SECTION*() {return &mutex;}
  112. };
  113. class CriticalScope {
  114. CriticalSection &mutex;
  115. CriticalScope() = delete;
  116. CriticalScope& operator=(CriticalScope &cs) = delete;
  117. public:
  118. inline CriticalScope(CriticalSection &mutex_) : mutex(mutex_)
  119. {
  120. EnterCriticalSection(mutex);
  121. }
  122. inline ~CriticalScope()
  123. {
  124. LeaveCriticalSection(mutex);
  125. }
  126. };
  127. enum class Action {
  128. None,
  129. Activate,
  130. Deactivate,
  131. Shutdown,
  132. ConfigVideo,
  133. ConfigAudio,
  134. ConfigCrossbar1,
  135. ConfigCrossbar2,
  136. };
  137. static DWORD CALLBACK DShowThread(LPVOID ptr);
  138. struct DShowInput {
  139. obs_source_t *source;
  140. Device device;
  141. bool deactivateWhenNotShowing = false;
  142. bool deviceHasAudio = false;
  143. bool flip = false;
  144. bool active = false;
  145. Decoder audio_decoder;
  146. Decoder video_decoder;
  147. VideoConfig videoConfig;
  148. AudioConfig audioConfig;
  149. obs_source_frame frame;
  150. obs_source_audio audio;
  151. WinHandle semaphore;
  152. WinHandle thread;
  153. CriticalSection mutex;
  154. vector<Action> actions;
  155. inline void QueueAction(Action action)
  156. {
  157. CriticalScope scope(mutex);
  158. actions.push_back(action);
  159. ReleaseSemaphore(semaphore, 1, nullptr);
  160. }
  161. inline DShowInput(obs_source_t *source_, obs_data_t *settings)
  162. : source (source_),
  163. device (InitGraph::False)
  164. {
  165. memset(&audio, 0, sizeof(audio));
  166. memset(&frame, 0, sizeof(frame));
  167. av_log_set_level(AV_LOG_WARNING);
  168. av_log_set_callback(ffmpeg_log);
  169. semaphore = CreateSemaphore(nullptr, 0, 0x7FFFFFFF, nullptr);
  170. if (!semaphore)
  171. throw "Failed to create semaphore";
  172. thread = CreateThread(nullptr, 0, DShowThread, this, 0,
  173. nullptr);
  174. if (!thread)
  175. throw "Failed to create thread";
  176. if (obs_data_get_bool(settings, "active")) {
  177. QueueAction(Action::Activate);
  178. active = true;
  179. }
  180. }
  181. inline ~DShowInput()
  182. {
  183. {
  184. CriticalScope scope(mutex);
  185. actions.resize(1);
  186. actions[0] = Action::Shutdown;
  187. }
  188. ReleaseSemaphore(semaphore, 1, nullptr);
  189. WaitForSingleObject(thread, INFINITE);
  190. }
  191. void OnEncodedVideoData(enum AVCodecID id,
  192. unsigned char *data, size_t size, long long ts);
  193. void OnEncodedAudioData(enum AVCodecID id,
  194. unsigned char *data, size_t size, long long ts);
  195. void OnVideoData(const VideoConfig &config,
  196. unsigned char *data, size_t size,
  197. long long startTime, long long endTime);
  198. void OnAudioData(const AudioConfig &config,
  199. unsigned char *data, size_t size,
  200. long long startTime, long long endTime);
  201. bool UpdateVideoConfig(obs_data_t *settings);
  202. bool UpdateAudioConfig(obs_data_t *settings);
  203. void SetActive(bool active);
  204. inline enum video_colorspace GetColorSpace(obs_data_t *settings) const;
  205. inline enum video_range_type GetColorRange(obs_data_t *settings) const;
  206. inline bool Activate(obs_data_t *settings);
  207. inline void Deactivate();
  208. inline void SetupBuffering(obs_data_t *settings);
  209. void DShowLoop();
  210. };
  211. static DWORD CALLBACK DShowThread(LPVOID ptr)
  212. {
  213. DShowInput *dshowInput = (DShowInput*)ptr;
  214. os_set_thread_name("win-dshow: DShowThread");
  215. CoInitialize(nullptr);
  216. dshowInput->DShowLoop();
  217. CoUninitialize();
  218. return 0;
  219. }
  220. static inline void ProcessMessages()
  221. {
  222. MSG msg;
  223. while (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) {
  224. TranslateMessage(&msg);
  225. DispatchMessage(&msg);
  226. }
  227. }
  228. /* Always keep directshow in a single thread for a given device */
  229. void DShowInput::DShowLoop()
  230. {
  231. while (true) {
  232. DWORD ret = MsgWaitForMultipleObjects(1, &semaphore, false,
  233. INFINITE, QS_ALLINPUT);
  234. if (ret == (WAIT_OBJECT_0 + 1)) {
  235. ProcessMessages();
  236. continue;
  237. } else if (ret != WAIT_OBJECT_0) {
  238. break;
  239. }
  240. Action action = Action::None;
  241. {
  242. CriticalScope scope(mutex);
  243. if (actions.size()) {
  244. action = actions.front();
  245. actions.erase(actions.begin());
  246. }
  247. }
  248. switch (action) {
  249. case Action::Activate:
  250. {
  251. obs_data_t *settings;
  252. settings = obs_source_get_settings(source);
  253. if (!Activate(settings)) {
  254. obs_source_output_video(source,
  255. nullptr);
  256. }
  257. obs_data_release(settings);
  258. break;
  259. }
  260. case Action::Deactivate:
  261. Deactivate();
  262. break;
  263. case Action::Shutdown:
  264. device.ShutdownGraph();
  265. return;
  266. case Action::ConfigVideo:
  267. device.OpenDialog(nullptr, DialogType::ConfigVideo);
  268. break;
  269. case Action::ConfigAudio:
  270. device.OpenDialog(nullptr, DialogType::ConfigAudio);
  271. break;
  272. case Action::ConfigCrossbar1:
  273. device.OpenDialog(nullptr, DialogType::ConfigCrossbar);
  274. break;
  275. case Action::ConfigCrossbar2:
  276. device.OpenDialog(nullptr, DialogType::ConfigCrossbar2);
  277. break;
  278. case Action::None:;
  279. }
  280. }
  281. }
  282. #define FPS_HIGHEST 0LL
  283. #define FPS_MATCHING -1LL
  284. template <typename T, typename U, typename V>
  285. static bool between(T &&lower, U &&value, V &&upper)
  286. {
  287. return value >= lower && value <= upper;
  288. }
  289. static bool ResolutionAvailable(const VideoInfo &cap, int cx, int cy)
  290. {
  291. return between(cap.minCX, cx, cap.maxCX) &&
  292. between(cap.minCY, cy, cap.maxCY);
  293. }
  294. #define DEVICE_INTERVAL_DIFF_LIMIT 20
  295. static bool FrameRateAvailable(const VideoInfo &cap, long long interval)
  296. {
  297. return interval == FPS_HIGHEST || interval == FPS_MATCHING ||
  298. between(cap.minInterval - DEVICE_INTERVAL_DIFF_LIMIT,
  299. interval,
  300. cap.maxInterval + DEVICE_INTERVAL_DIFF_LIMIT);
  301. }
  302. static long long FrameRateInterval(const VideoInfo &cap,
  303. long long desired_interval)
  304. {
  305. return desired_interval < cap.minInterval ?
  306. cap.minInterval :
  307. min(desired_interval, cap.maxInterval);
  308. }
  309. static inline video_format ConvertVideoFormat(VideoFormat format)
  310. {
  311. switch (format) {
  312. case VideoFormat::ARGB: return VIDEO_FORMAT_BGRA;
  313. case VideoFormat::XRGB: return VIDEO_FORMAT_BGRX;
  314. case VideoFormat::I420: return VIDEO_FORMAT_I420;
  315. case VideoFormat::YV12: return VIDEO_FORMAT_I420;
  316. case VideoFormat::NV12: return VIDEO_FORMAT_NV12;
  317. case VideoFormat::Y800: return VIDEO_FORMAT_Y800;
  318. case VideoFormat::YVYU: return VIDEO_FORMAT_YVYU;
  319. case VideoFormat::YUY2: return VIDEO_FORMAT_YUY2;
  320. case VideoFormat::UYVY: return VIDEO_FORMAT_UYVY;
  321. case VideoFormat::HDYC: return VIDEO_FORMAT_UYVY;
  322. case VideoFormat::MJPEG: return VIDEO_FORMAT_YUY2;
  323. default: return VIDEO_FORMAT_NONE;
  324. }
  325. }
  326. static inline audio_format ConvertAudioFormat(AudioFormat format)
  327. {
  328. switch (format) {
  329. case AudioFormat::Wave16bit: return AUDIO_FORMAT_16BIT;
  330. case AudioFormat::WaveFloat: return AUDIO_FORMAT_FLOAT;
  331. default: return AUDIO_FORMAT_UNKNOWN;
  332. }
  333. }
  334. //#define LOG_ENCODED_VIDEO_TS 1
  335. //#define LOG_ENCODED_AUDIO_TS 1
  336. void DShowInput::OnEncodedVideoData(enum AVCodecID id,
  337. unsigned char *data, size_t size, long long ts)
  338. {
  339. if (!ffmpeg_decode_valid(video_decoder)) {
  340. if (ffmpeg_decode_init(video_decoder, id) < 0) {
  341. blog(LOG_WARNING, "Could not initialize video decoder");
  342. return;
  343. }
  344. }
  345. bool got_output;
  346. int len = ffmpeg_decode_video(video_decoder, data, size, &ts,
  347. &frame, &got_output);
  348. if (len < 0) {
  349. blog(LOG_WARNING, "Error decoding video");
  350. return;
  351. }
  352. if (got_output) {
  353. frame.timestamp = (uint64_t)ts * 100;
  354. if (flip)
  355. frame.flip = !frame.flip;
  356. #if LOG_ENCODED_VIDEO_TS
  357. blog(LOG_DEBUG, "video ts: %llu", frame.timestamp);
  358. #endif
  359. obs_source_output_video(source, &frame);
  360. }
  361. }
  362. void DShowInput::OnVideoData(const VideoConfig &config,
  363. unsigned char *data, size_t size,
  364. long long startTime, long long endTime)
  365. {
  366. if (videoConfig.format == VideoFormat::H264) {
  367. OnEncodedVideoData(AV_CODEC_ID_H264, data, size, startTime);
  368. return;
  369. }
  370. const int cx = config.cx;
  371. const int cy = config.cy;
  372. frame.timestamp = (uint64_t)startTime * 100;
  373. frame.width = config.cx;
  374. frame.height = config.cy;
  375. frame.format = ConvertVideoFormat(config.format);
  376. frame.flip = (config.format == VideoFormat::XRGB ||
  377. config.format == VideoFormat::ARGB);
  378. if (flip)
  379. frame.flip = !frame.flip;
  380. if (videoConfig.format == VideoFormat::XRGB ||
  381. videoConfig.format == VideoFormat::ARGB) {
  382. frame.data[0] = data;
  383. frame.linesize[0] = cx * 4;
  384. } else if (videoConfig.format == VideoFormat::YVYU ||
  385. videoConfig.format == VideoFormat::YUY2 ||
  386. videoConfig.format == VideoFormat::HDYC ||
  387. videoConfig.format == VideoFormat::UYVY) {
  388. frame.data[0] = data;
  389. frame.linesize[0] = cx * 2;
  390. } else if (videoConfig.format == VideoFormat::I420) {
  391. frame.data[0] = data;
  392. frame.data[1] = frame.data[0] + (cx * cy);
  393. frame.data[2] = frame.data[1] + (cx * cy / 4);
  394. frame.linesize[0] = cx;
  395. frame.linesize[1] = cx / 2;
  396. frame.linesize[2] = cx / 2;
  397. } else if (videoConfig.format == VideoFormat::YV12) {
  398. frame.data[0] = data;
  399. frame.data[2] = frame.data[0] + (cx * cy);
  400. frame.data[1] = frame.data[2] + (cx * cy / 4);
  401. frame.linesize[0] = cx;
  402. frame.linesize[1] = cx / 2;
  403. frame.linesize[2] = cx / 2;
  404. } else if (videoConfig.format == VideoFormat::NV12) {
  405. frame.data[0] = data;
  406. frame.data[1] = frame.data[0] + (cx * cy);
  407. frame.linesize[0] = cx;
  408. frame.linesize[1] = cx;
  409. } else if (videoConfig.format == VideoFormat::Y800) {
  410. frame.data[0] = data;
  411. frame.linesize[0] = cx;
  412. } else {
  413. /* TODO: other formats */
  414. return;
  415. }
  416. obs_source_output_video(source, &frame);
  417. UNUSED_PARAMETER(endTime); /* it's the enndd tiimmes! */
  418. UNUSED_PARAMETER(size);
  419. }
  420. void DShowInput::OnEncodedAudioData(enum AVCodecID id,
  421. unsigned char *data, size_t size, long long ts)
  422. {
  423. if (!ffmpeg_decode_valid(audio_decoder)) {
  424. if (ffmpeg_decode_init(audio_decoder, id) < 0) {
  425. blog(LOG_WARNING, "Could not initialize audio decoder");
  426. return;
  427. }
  428. if (videoConfig.name.find(L"C875") != std::string::npos ||
  429. videoConfig.name.find(L"C835") != std::string::npos) {
  430. audio_decoder->fix_braindead_lgp_audio_packet_stupidity
  431. = true;
  432. blog(LOG_INFO, "Oh great, an LGP was detected. "
  433. "How wonderful. I'm just ecstatic.");
  434. }
  435. }
  436. bool got_output;
  437. int len = ffmpeg_decode_audio(audio_decoder, data, size, &ts,
  438. &audio, &got_output);
  439. if (len < 0) {
  440. blog(LOG_WARNING, "Error decoding audio");
  441. return;
  442. }
  443. if (got_output) {
  444. audio.timestamp = (uint64_t)ts * 100;
  445. #if LOG_ENCODED_AUDIO_TS
  446. blog(LOG_DEBUG, "audio ts: %llu", audio.timestamp);
  447. #endif
  448. obs_source_output_audio(source, &audio);
  449. }
  450. }
  451. void DShowInput::OnAudioData(const AudioConfig &config,
  452. unsigned char *data, size_t size,
  453. long long startTime, long long endTime)
  454. {
  455. size_t block_size;
  456. if (config.format == AudioFormat::AAC) {
  457. OnEncodedAudioData(AV_CODEC_ID_AAC, data, size, startTime);
  458. return;
  459. } else if (config.format == AudioFormat::AC3) {
  460. OnEncodedAudioData(AV_CODEC_ID_AC3, data, size, startTime);
  461. return;
  462. } else if (config.format == AudioFormat::MPGA) {
  463. OnEncodedAudioData(AV_CODEC_ID_MP2, data, size, startTime);
  464. return;
  465. }
  466. audio.speakers = (enum speaker_layout)config.channels;
  467. audio.format = ConvertAudioFormat(config.format);
  468. audio.samples_per_sec = (uint32_t)config.sampleRate;
  469. audio.data[0] = data;
  470. block_size = get_audio_bytes_per_channel(audio.format) *
  471. get_audio_channels(audio.speakers);
  472. audio.frames = (uint32_t)(size / block_size);
  473. audio.timestamp = (uint64_t)startTime * 100;
  474. if (audio.format != AUDIO_FORMAT_UNKNOWN)
  475. obs_source_output_audio(source, &audio);
  476. UNUSED_PARAMETER(endTime);
  477. }
  478. struct PropertiesData {
  479. DShowInput *input;
  480. vector<VideoDevice> devices;
  481. vector<AudioDevice> audioDevices;
  482. bool GetDevice(VideoDevice &device, const char *encoded_id) const
  483. {
  484. DeviceId deviceId;
  485. DecodeDeviceId(deviceId, encoded_id);
  486. for (const VideoDevice &curDevice : devices) {
  487. if (deviceId.name == curDevice.name &&
  488. deviceId.path == curDevice.path) {
  489. device = curDevice;
  490. return true;
  491. }
  492. }
  493. return false;
  494. }
  495. };
  496. static inline bool ConvertRes(int &cx, int &cy, const char *res)
  497. {
  498. return sscanf(res, "%dx%d", &cx, &cy) == 2;
  499. }
  500. static inline bool FormatMatches(VideoFormat left, VideoFormat right)
  501. {
  502. return left == VideoFormat::Any || right == VideoFormat::Any ||
  503. left == right;
  504. }
  505. static inline bool ResolutionValid(string res, int &cx, int &cy)
  506. {
  507. if (!res.size())
  508. return false;
  509. return ConvertRes(cx, cy, res.c_str());
  510. }
  511. static inline bool CapsMatch(const VideoInfo&)
  512. {
  513. return true;
  514. }
  515. template <typename ... F>
  516. static bool CapsMatch(const VideoDevice &dev, F ... fs);
  517. template <typename F, typename ... Fs>
  518. static inline bool CapsMatch(const VideoInfo &info, F&& f, Fs ... fs)
  519. {
  520. return f(info) && CapsMatch(info, fs ...);
  521. }
  522. template <typename ... F>
  523. static bool CapsMatch(const VideoDevice &dev, F ... fs)
  524. {
  525. auto matcher = [&](const VideoInfo &info)
  526. {
  527. return CapsMatch(info, fs ...);
  528. };
  529. return any_of(begin(dev.caps), end(dev.caps), matcher);
  530. }
  531. static inline bool MatcherMatchVideoFormat(VideoFormat format,
  532. bool &did_match, const VideoInfo &info)
  533. {
  534. bool match = FormatMatches(format, info.format);
  535. did_match = did_match || match;
  536. return match;
  537. }
  538. static inline bool MatcherClosestFrameRateSelector(long long interval,
  539. long long &best_match, const VideoInfo &info)
  540. {
  541. long long current = FrameRateInterval(info, interval);
  542. if (llabs(interval - best_match) > llabs(interval - current))
  543. best_match = current;
  544. return true;
  545. }
  546. #if 0
  547. auto ResolutionMatcher = [](int cx, int cy)
  548. {
  549. return [cx, cy](const VideoInfo &info)
  550. {
  551. return ResolutionAvailable(info, cx, cy);
  552. };
  553. };
  554. auto FrameRateMatcher = [](long long interval)
  555. {
  556. return [interval](const VideoInfo &info)
  557. {
  558. return FrameRateAvailable(info, interval);
  559. };
  560. };
  561. auto VideoFormatMatcher = [](VideoFormat format, bool &did_match)
  562. {
  563. return [format, &did_match](const VideoInfo &info)
  564. {
  565. return MatcherMatchVideoFormat(format, did_match, info);
  566. };
  567. };
  568. auto ClosestFrameRateSelector = [](long long interval, long long &best_match)
  569. {
  570. return [interval, &best_match](const VideoInfo &info) mutable -> bool
  571. {
  572. MatcherClosestFrameRateSelector(interval, best_match, info);
  573. };
  574. }
  575. #else
  576. #define ResolutionMatcher(cx, cy) \
  577. [cx, cy](const VideoInfo &info) -> bool \
  578. { return ResolutionAvailable(info, cx, cy); }
  579. #define FrameRateMatcher(interval) \
  580. [interval](const VideoInfo &info) -> bool \
  581. { return FrameRateAvailable(info, interval); }
  582. #define VideoFormatMatcher(format, did_match) \
  583. [format, &did_match](const VideoInfo &info) mutable -> bool \
  584. { return MatcherMatchVideoFormat(format, did_match, info); }
  585. #define ClosestFrameRateSelector(interval, best_match) \
  586. [interval, &best_match](const VideoInfo &info) mutable -> bool \
  587. { return MatcherClosestFrameRateSelector(interval, best_match, info); }
  588. #endif
  589. static bool ResolutionAvailable(const VideoDevice &dev, int cx, int cy)
  590. {
  591. return CapsMatch(dev, ResolutionMatcher(cx, cy));
  592. }
  593. static bool DetermineResolution(int &cx, int &cy, obs_data_t *settings,
  594. VideoDevice dev)
  595. {
  596. const char *res = obs_data_get_autoselect_string(settings, RESOLUTION);
  597. if (obs_data_has_autoselect_value(settings, RESOLUTION) &&
  598. ConvertRes(cx, cy, res) &&
  599. ResolutionAvailable(dev, cx, cy))
  600. return true;
  601. res = obs_data_get_string(settings, RESOLUTION);
  602. if (ConvertRes(cx, cy, res) && ResolutionAvailable(dev, cx, cy))
  603. return true;
  604. res = obs_data_get_string(settings, LAST_RESOLUTION);
  605. if (ConvertRes(cx, cy, res) && ResolutionAvailable(dev, cx, cy))
  606. return true;
  607. return false;
  608. }
  609. static long long GetOBSFPS();
  610. static inline bool IsEncoded(const VideoConfig &config)
  611. {
  612. return config.format >= VideoFormat::MJPEG ||
  613. wstrstri(config.name.c_str(), L"elgato") != NULL ||
  614. wstrstri(config.name.c_str(), L"stream engine") != NULL;
  615. }
  616. inline void DShowInput::SetupBuffering(obs_data_t *settings)
  617. {
  618. BufferingType bufType;
  619. uint32_t flags = obs_source_get_flags(source);
  620. bool useBuffering;
  621. bufType = (BufferingType)obs_data_get_int(settings, BUFFERING_VAL);
  622. if (bufType == BufferingType::Auto)
  623. useBuffering = IsEncoded(videoConfig);
  624. else
  625. useBuffering = bufType == BufferingType::On;
  626. if (useBuffering)
  627. flags &= ~OBS_SOURCE_FLAG_UNBUFFERED;
  628. else
  629. flags |= OBS_SOURCE_FLAG_UNBUFFERED;
  630. obs_source_set_flags(source, flags);
  631. }
  632. static DStr GetVideoFormatName(VideoFormat format);
  633. bool DShowInput::UpdateVideoConfig(obs_data_t *settings)
  634. {
  635. string video_device_id = obs_data_get_string(settings, VIDEO_DEVICE_ID);
  636. deactivateWhenNotShowing = obs_data_get_bool(settings, DEACTIVATE_WNS);
  637. flip = obs_data_get_bool(settings, FLIP_IMAGE);
  638. DeviceId id;
  639. if (!DecodeDeviceId(id, video_device_id.c_str())) {
  640. blog(LOG_WARNING, "%s: DecodeDeviceId failed",
  641. obs_source_get_name(source));
  642. return false;
  643. }
  644. PropertiesData data;
  645. Device::EnumVideoDevices(data.devices);
  646. VideoDevice dev;
  647. if (!data.GetDevice(dev, video_device_id.c_str())) {
  648. blog(LOG_WARNING, "%s: data.GetDevice failed",
  649. obs_source_get_name(source));
  650. return false;
  651. }
  652. int resType = (int)obs_data_get_int(settings, RES_TYPE);
  653. int cx = 0, cy = 0;
  654. long long interval = 0;
  655. VideoFormat format = VideoFormat::Any;
  656. if (resType == ResType_Custom) {
  657. bool has_autosel_val;
  658. string resolution = obs_data_get_string(settings, RESOLUTION);
  659. if (!ResolutionValid(resolution, cx, cy)) {
  660. blog(LOG_WARNING, "%s: ResolutionValid failed",
  661. obs_source_get_name(source));
  662. return false;
  663. }
  664. has_autosel_val = obs_data_has_autoselect_value(settings,
  665. FRAME_INTERVAL);
  666. interval = has_autosel_val ?
  667. obs_data_get_autoselect_int(settings, FRAME_INTERVAL) :
  668. obs_data_get_int(settings, FRAME_INTERVAL);
  669. if (interval == FPS_MATCHING)
  670. interval = GetOBSFPS();
  671. format = (VideoFormat)obs_data_get_int(settings, VIDEO_FORMAT);
  672. long long best_interval = numeric_limits<long long>::max();
  673. bool video_format_match = false;
  674. bool caps_match = CapsMatch(dev, ResolutionMatcher(cx, cy),
  675. VideoFormatMatcher(format, video_format_match),
  676. ClosestFrameRateSelector(interval, best_interval),
  677. FrameRateMatcher(interval));
  678. if (!caps_match && !video_format_match) {
  679. blog(LOG_WARNING, "%s: Video format match failed",
  680. obs_source_get_name(source));
  681. return false;
  682. }
  683. interval = best_interval;
  684. }
  685. videoConfig.name = id.name.c_str();
  686. videoConfig.path = id.path.c_str();
  687. videoConfig.useDefaultConfig = resType == ResType_Preferred;
  688. videoConfig.cx = cx;
  689. videoConfig.cy = cy;
  690. videoConfig.frameInterval = interval;
  691. videoConfig.internalFormat = format;
  692. deviceHasAudio = dev.audioAttached;
  693. videoConfig.callback = std::bind(&DShowInput::OnVideoData, this,
  694. placeholders::_1, placeholders::_2,
  695. placeholders::_3, placeholders::_4,
  696. placeholders::_5);
  697. if (videoConfig.internalFormat != VideoFormat::MJPEG)
  698. videoConfig.format = videoConfig.internalFormat;
  699. if (!device.SetVideoConfig(&videoConfig)) {
  700. blog(LOG_WARNING, "%s: device.SetVideoConfig failed",
  701. obs_source_get_name(source));
  702. return false;
  703. }
  704. if (videoConfig.internalFormat == VideoFormat::MJPEG) {
  705. videoConfig.format = VideoFormat::XRGB;
  706. if (!device.SetVideoConfig(&videoConfig)) {
  707. blog(LOG_WARNING, "%s: device.SetVideoConfig (XRGB) "
  708. "failed", obs_source_get_name(source));
  709. return false;
  710. }
  711. }
  712. DStr formatName = GetVideoFormatName(videoConfig.internalFormat);
  713. double fps = 0.0;
  714. if (videoConfig.frameInterval)
  715. fps = 10000000.0 / double(videoConfig.frameInterval);
  716. BPtr<char> name_utf8;
  717. BPtr<char> path_utf8;
  718. os_wcs_to_utf8_ptr(videoConfig.name.c_str(), videoConfig.name.size(),
  719. &name_utf8);
  720. os_wcs_to_utf8_ptr(videoConfig.path.c_str(), videoConfig.path.size(),
  721. &path_utf8);
  722. blog(LOG_INFO, "---------------------------------");
  723. blog(LOG_INFO, "[DShow Device: '%s'] settings updated: \n"
  724. "\tvideo device: %s\n"
  725. "\tvideo path: %s\n"
  726. "\tresolution: %dx%d\n"
  727. "\tfps: %0.2f (interval: %lld)\n"
  728. "\tformat: %s",
  729. obs_source_get_name(source),
  730. (const char*)name_utf8,
  731. (const char*)path_utf8,
  732. videoConfig.cx, videoConfig.cy,
  733. fps, videoConfig.frameInterval,
  734. formatName->array);
  735. SetupBuffering(settings);
  736. return true;
  737. }
  738. bool DShowInput::UpdateAudioConfig(obs_data_t *settings)
  739. {
  740. string audio_device_id = obs_data_get_string(settings, AUDIO_DEVICE_ID);
  741. bool useCustomAudio = obs_data_get_bool(settings, USE_CUSTOM_AUDIO);
  742. if (useCustomAudio) {
  743. DeviceId id;
  744. if (!DecodeDeviceId(id, audio_device_id.c_str()))
  745. return false;
  746. audioConfig.name = id.name.c_str();
  747. audioConfig.path = id.path.c_str();
  748. } else if (!deviceHasAudio) {
  749. return true;
  750. }
  751. audioConfig.useVideoDevice = !useCustomAudio;
  752. audioConfig.callback = std::bind(&DShowInput::OnAudioData, this,
  753. placeholders::_1, placeholders::_2,
  754. placeholders::_3, placeholders::_4,
  755. placeholders::_5);
  756. audioConfig.mode =
  757. (AudioMode)obs_data_get_int(settings, AUDIO_OUTPUT_MODE);
  758. bool success = device.SetAudioConfig(&audioConfig);
  759. if (!success)
  760. return false;
  761. BPtr<char> name_utf8;
  762. os_wcs_to_utf8_ptr(audioConfig.name.c_str(), audioConfig.name.size(),
  763. &name_utf8);
  764. blog(LOG_INFO, "\tusing video device audio: %s",
  765. audioConfig.useVideoDevice ? "yes" : "no");
  766. if (!audioConfig.useVideoDevice)
  767. blog(LOG_INFO, "\taudio device: %s", (const char*)name_utf8);
  768. const char *mode = "";
  769. switch (audioConfig.mode) {
  770. case AudioMode::Capture: mode = "Capture"; break;
  771. case AudioMode::DirectSound: mode = "DirectSound"; break;
  772. case AudioMode::WaveOut: mode = "WaveOut"; break;
  773. }
  774. blog(LOG_INFO, "\tsample rate: %d\n"
  775. "\tchannels: %d\n"
  776. "\taudio type: %s",
  777. audioConfig.sampleRate,
  778. audioConfig.channels,
  779. mode);
  780. return true;
  781. }
  782. void DShowInput::SetActive(bool active_)
  783. {
  784. obs_data_t *settings = obs_source_get_settings(source);
  785. QueueAction(active_ ? Action::Activate : Action::Deactivate);
  786. obs_data_set_bool(settings, "active", active_);
  787. active = active_;
  788. obs_data_release(settings);
  789. }
  790. inline enum video_colorspace DShowInput::GetColorSpace(
  791. obs_data_t *settings) const
  792. {
  793. const char *space = obs_data_get_string(settings, COLOR_SPACE);
  794. if (astrcmpi(space, "709") == 0)
  795. return VIDEO_CS_709;
  796. else if (astrcmpi(space, "601") == 0)
  797. return VIDEO_CS_601;
  798. else
  799. return (videoConfig.format == VideoFormat::HDYC) ?
  800. VIDEO_CS_709 : VIDEO_CS_601;
  801. }
  802. inline enum video_range_type DShowInput::GetColorRange(
  803. obs_data_t *settings) const
  804. {
  805. const char *range = obs_data_get_string(settings, COLOR_RANGE);
  806. return astrcmpi(range, "full") == 0 ?
  807. VIDEO_RANGE_FULL : VIDEO_RANGE_PARTIAL;
  808. }
  809. inline bool DShowInput::Activate(obs_data_t *settings)
  810. {
  811. if (!device.ResetGraph())
  812. return false;
  813. if (!UpdateVideoConfig(settings)) {
  814. blog(LOG_WARNING, "%s: Video configuration failed",
  815. obs_source_get_name(source));
  816. return false;
  817. }
  818. if (!UpdateAudioConfig(settings))
  819. blog(LOG_WARNING, "%s: Audio configuration failed, ignoring "
  820. "audio", obs_source_get_name(source));
  821. if (!device.ConnectFilters())
  822. return false;
  823. enum video_colorspace cs = GetColorSpace(settings);
  824. video_range_type range = GetColorRange(settings);
  825. frame.full_range = range == VIDEO_RANGE_FULL;
  826. if (device.Start() != Result::Success)
  827. return false;
  828. bool success = video_format_get_parameters(
  829. cs, range,
  830. frame.color_matrix,
  831. frame.color_range_min,
  832. frame.color_range_max);
  833. if (!success) {
  834. blog(LOG_ERROR, "Failed to get video format parameters for " \
  835. "video format %u", cs);
  836. }
  837. return true;
  838. }
  839. inline void DShowInput::Deactivate()
  840. {
  841. device.ResetGraph();
  842. obs_source_output_video(source, nullptr);
  843. }
  844. /* ------------------------------------------------------------------------- */
  845. static const char *GetDShowInputName(void*)
  846. {
  847. return TEXT_INPUT_NAME;
  848. }
  849. static void *CreateDShowInput(obs_data_t *settings, obs_source_t *source)
  850. {
  851. DShowInput *dshow = nullptr;
  852. try {
  853. dshow = new DShowInput(source, settings);
  854. } catch (const char *error) {
  855. blog(LOG_ERROR, "Could not create device '%s': %s",
  856. obs_source_get_name(source), error);
  857. }
  858. return dshow;
  859. }
  860. static void DestroyDShowInput(void *data)
  861. {
  862. delete reinterpret_cast<DShowInput*>(data);
  863. }
  864. static void UpdateDShowInput(void *data, obs_data_t *settings)
  865. {
  866. DShowInput *input = reinterpret_cast<DShowInput*>(data);
  867. if (input->active)
  868. input->QueueAction(Action::Activate);
  869. UNUSED_PARAMETER(settings);
  870. }
  871. static void GetDShowDefaults(obs_data_t *settings)
  872. {
  873. obs_data_set_default_int(settings, FRAME_INTERVAL, FPS_MATCHING);
  874. obs_data_set_default_int(settings, RES_TYPE, ResType_Preferred);
  875. obs_data_set_default_int(settings, VIDEO_FORMAT, (int)VideoFormat::Any);
  876. obs_data_set_default_bool(settings, "active", true);
  877. obs_data_set_default_string(settings, COLOR_SPACE, "default");
  878. obs_data_set_default_string(settings, COLOR_RANGE, "partial");
  879. obs_data_set_default_int(settings, AUDIO_OUTPUT_MODE,
  880. (int)AudioMode::Capture);
  881. }
  882. struct Resolution {
  883. int cx, cy;
  884. inline Resolution(int cx, int cy) : cx(cx), cy(cy) {}
  885. };
  886. static void InsertResolution(vector<Resolution> &resolutions, int cx, int cy)
  887. {
  888. int bestCY = 0;
  889. size_t idx = 0;
  890. for (; idx < resolutions.size(); idx++) {
  891. const Resolution &res = resolutions[idx];
  892. if (res.cx > cx)
  893. break;
  894. if (res.cx == cx) {
  895. if (res.cy == cy)
  896. return;
  897. if (!bestCY)
  898. bestCY = res.cy;
  899. else if (res.cy > bestCY)
  900. break;
  901. }
  902. }
  903. resolutions.insert(resolutions.begin() + idx, Resolution(cx, cy));
  904. }
  905. static inline void AddCap(vector<Resolution> &resolutions, const VideoInfo &cap)
  906. {
  907. InsertResolution(resolutions, cap.minCX, cap.minCY);
  908. InsertResolution(resolutions, cap.maxCX, cap.maxCY);
  909. }
  910. #define MAKE_DSHOW_FPS(fps) (10000000LL/(fps))
  911. #define MAKE_DSHOW_FRACTIONAL_FPS(den, num) ((num)*10000000LL/(den))
  912. static long long GetOBSFPS()
  913. {
  914. obs_video_info ovi;
  915. if (!obs_get_video_info(&ovi))
  916. return 0;
  917. return MAKE_DSHOW_FRACTIONAL_FPS(ovi.fps_num, ovi.fps_den);
  918. }
  919. struct FPSFormat {
  920. const char *text;
  921. long long interval;
  922. };
  923. static const FPSFormat validFPSFormats[] = {
  924. {"60", MAKE_DSHOW_FPS(60)},
  925. {"59.94 NTSC", MAKE_DSHOW_FRACTIONAL_FPS(60000, 1001)},
  926. {"50", MAKE_DSHOW_FPS(50)},
  927. {"48 film", MAKE_DSHOW_FRACTIONAL_FPS(48000, 1001)},
  928. {"40", MAKE_DSHOW_FPS(40)},
  929. {"30", MAKE_DSHOW_FPS(30)},
  930. {"29.97 NTSC", MAKE_DSHOW_FRACTIONAL_FPS(30000, 1001)},
  931. {"25", MAKE_DSHOW_FPS(25)},
  932. {"24 film", MAKE_DSHOW_FRACTIONAL_FPS(24000, 1001)},
  933. {"20", MAKE_DSHOW_FPS(20)},
  934. {"15", MAKE_DSHOW_FPS(15)},
  935. {"10", MAKE_DSHOW_FPS(10)},
  936. {"5", MAKE_DSHOW_FPS(5)},
  937. {"4", MAKE_DSHOW_FPS(4)},
  938. {"3", MAKE_DSHOW_FPS(3)},
  939. {"2", MAKE_DSHOW_FPS(2)},
  940. {"1", MAKE_DSHOW_FPS(1)},
  941. };
  942. static bool DeviceIntervalChanged(obs_properties_t *props, obs_property_t *p,
  943. obs_data_t *settings);
  944. static bool TryResolution(VideoDevice &dev, string res)
  945. {
  946. int cx, cy;
  947. if (!ConvertRes(cx, cy, res.c_str()))
  948. return false;
  949. return ResolutionAvailable(dev, cx, cy);
  950. }
  951. static bool SetResolution(obs_properties_t *props, obs_data_t *settings,
  952. string res, bool autoselect=false)
  953. {
  954. if (autoselect)
  955. obs_data_set_autoselect_string(settings, RESOLUTION,
  956. res.c_str());
  957. else
  958. obs_data_unset_autoselect_value(settings, RESOLUTION);
  959. DeviceIntervalChanged(props, obs_properties_get(props, FRAME_INTERVAL),
  960. settings);
  961. if (!autoselect)
  962. obs_data_set_string(settings, LAST_RESOLUTION, res.c_str());
  963. return true;
  964. }
  965. static bool DeviceResolutionChanged(obs_properties_t *props, obs_property_t *p,
  966. obs_data_t *settings)
  967. {
  968. UNUSED_PARAMETER(p);
  969. PropertiesData *data = (PropertiesData*)obs_properties_get_param(props);
  970. const char *id;
  971. VideoDevice device;
  972. id = obs_data_get_string(settings, VIDEO_DEVICE_ID);
  973. string res = obs_data_get_string(settings, RESOLUTION);
  974. string last_res = obs_data_get_string(settings, LAST_RESOLUTION);
  975. if (!data->GetDevice(device, id))
  976. return false;
  977. if (TryResolution(device, res))
  978. return SetResolution(props, settings, res);
  979. if (TryResolution(device, last_res))
  980. return SetResolution(props, settings, last_res, true);
  981. return false;
  982. }
  983. struct VideoFormatName {
  984. VideoFormat format;
  985. const char *name;
  986. };
  987. static const VideoFormatName videoFormatNames[] = {
  988. /* autoselect format*/
  989. {VideoFormat::Any, "VideoFormat.Any"},
  990. /* raw formats */
  991. {VideoFormat::ARGB, "ARGB"},
  992. {VideoFormat::XRGB, "XRGB"},
  993. /* planar YUV formats */
  994. {VideoFormat::I420, "I420"},
  995. {VideoFormat::NV12, "NV12"},
  996. {VideoFormat::YV12, "YV12"},
  997. {VideoFormat::Y800, "Y800"},
  998. /* packed YUV formats */
  999. {VideoFormat::YVYU, "YVYU"},
  1000. {VideoFormat::YUY2, "YUY2"},
  1001. {VideoFormat::UYVY, "UYVY"},
  1002. {VideoFormat::HDYC, "HDYC"},
  1003. /* encoded formats */
  1004. {VideoFormat::MJPEG, "MJPEG"},
  1005. {VideoFormat::H264, "H264"}
  1006. };
  1007. static bool ResTypeChanged(obs_properties_t *props, obs_property_t *p,
  1008. obs_data_t *settings);
  1009. static size_t AddDevice(obs_property_t *device_list, const string &id)
  1010. {
  1011. DStr name, path;
  1012. if (!DecodeDeviceDStr(name, path, id.c_str()))
  1013. return numeric_limits<size_t>::max();
  1014. return obs_property_list_add_string(device_list, name, id.c_str());
  1015. }
  1016. static bool UpdateDeviceList(obs_property_t *list, const string &id)
  1017. {
  1018. size_t size = obs_property_list_item_count(list);
  1019. bool found = false;
  1020. bool disabled_unknown_found = false;
  1021. for (size_t i = 0; i < size; i++) {
  1022. if (obs_property_list_item_string(list, i) == id) {
  1023. found = true;
  1024. continue;
  1025. }
  1026. if (obs_property_list_item_disabled(list, i))
  1027. disabled_unknown_found = true;
  1028. }
  1029. if (!found && !disabled_unknown_found) {
  1030. size_t idx = AddDevice(list, id);
  1031. obs_property_list_item_disable(list, idx, true);
  1032. return true;
  1033. }
  1034. if (found && !disabled_unknown_found)
  1035. return false;
  1036. for (size_t i = 0; i < size;) {
  1037. if (obs_property_list_item_disabled(list, i)) {
  1038. obs_property_list_item_remove(list, i);
  1039. continue;
  1040. }
  1041. i += 1;
  1042. }
  1043. return true;
  1044. }
  1045. static bool DeviceSelectionChanged(obs_properties_t *props, obs_property_t *p,
  1046. obs_data_t *settings)
  1047. {
  1048. PropertiesData *data = (PropertiesData*)obs_properties_get_param(props);
  1049. VideoDevice device;
  1050. string id = obs_data_get_string(settings, VIDEO_DEVICE_ID);
  1051. string old_id = obs_data_get_string(settings, LAST_VIDEO_DEV_ID);
  1052. bool device_list_updated = UpdateDeviceList(p, id);
  1053. if (!data->GetDevice(device, id.c_str()))
  1054. return !device_list_updated;
  1055. vector<Resolution> resolutions;
  1056. for (const VideoInfo &cap : device.caps)
  1057. AddCap(resolutions, cap);
  1058. p = obs_properties_get(props, RESOLUTION);
  1059. obs_property_list_clear(p);
  1060. for (size_t idx = resolutions.size(); idx > 0; idx--) {
  1061. const Resolution &res = resolutions[idx-1];
  1062. string strRes;
  1063. strRes += to_string(res.cx);
  1064. strRes += "x";
  1065. strRes += to_string(res.cy);
  1066. obs_property_list_add_string(p, strRes.c_str(), strRes.c_str());
  1067. }
  1068. /* only refresh properties if device legitimately changed */
  1069. if (!id.size() || !old_id.size() || id != old_id) {
  1070. p = obs_properties_get(props, RES_TYPE);
  1071. ResTypeChanged(props, p, settings);
  1072. obs_data_set_string(settings, LAST_VIDEO_DEV_ID, id.c_str());
  1073. }
  1074. return true;
  1075. }
  1076. static bool VideoConfigClicked(obs_properties_t *props, obs_property_t *p,
  1077. void *data)
  1078. {
  1079. DShowInput *input = reinterpret_cast<DShowInput*>(data);
  1080. input->QueueAction(Action::ConfigVideo);
  1081. UNUSED_PARAMETER(props);
  1082. UNUSED_PARAMETER(p);
  1083. return false;
  1084. }
  1085. /*static bool AudioConfigClicked(obs_properties_t *props, obs_property_t *p,
  1086. void *data)
  1087. {
  1088. DShowInput *input = reinterpret_cast<DShowInput*>(data);
  1089. input->QueueAction(Action::ConfigAudio);
  1090. UNUSED_PARAMETER(props);
  1091. UNUSED_PARAMETER(p);
  1092. return false;
  1093. }*/
  1094. static bool CrossbarConfigClicked(obs_properties_t *props, obs_property_t *p,
  1095. void *data)
  1096. {
  1097. DShowInput *input = reinterpret_cast<DShowInput*>(data);
  1098. input->QueueAction(Action::ConfigCrossbar1);
  1099. UNUSED_PARAMETER(props);
  1100. UNUSED_PARAMETER(p);
  1101. return false;
  1102. }
  1103. /*static bool Crossbar2ConfigClicked(obs_properties_t *props, obs_property_t *p,
  1104. void *data)
  1105. {
  1106. DShowInput *input = reinterpret_cast<DShowInput*>(data);
  1107. input->QueueAction(Action::ConfigCrossbar2);
  1108. UNUSED_PARAMETER(props);
  1109. UNUSED_PARAMETER(p);
  1110. return false;
  1111. }*/
  1112. static bool AddDevice(obs_property_t *device_list, const VideoDevice &device)
  1113. {
  1114. DStr name, path, device_id;
  1115. dstr_from_wcs(name, device.name.c_str());
  1116. dstr_from_wcs(path, device.path.c_str());
  1117. encode_dstr(path);
  1118. dstr_copy_dstr(device_id, name);
  1119. encode_dstr(device_id);
  1120. dstr_cat(device_id, ":");
  1121. dstr_cat_dstr(device_id, path);
  1122. obs_property_list_add_string(device_list, name, device_id);
  1123. return true;
  1124. }
  1125. static bool AddAudioDevice(obs_property_t *device_list,
  1126. const AudioDevice &device)
  1127. {
  1128. DStr name, path, device_id;
  1129. dstr_from_wcs(name, device.name.c_str());
  1130. dstr_from_wcs(path, device.path.c_str());
  1131. encode_dstr(path);
  1132. dstr_copy_dstr(device_id, name);
  1133. encode_dstr(device_id);
  1134. dstr_cat(device_id, ":");
  1135. dstr_cat_dstr(device_id, path);
  1136. obs_property_list_add_string(device_list, name, device_id);
  1137. return true;
  1138. }
  1139. static void PropertiesDataDestroy(void *data)
  1140. {
  1141. delete reinterpret_cast<PropertiesData*>(data);
  1142. }
  1143. static bool ResTypeChanged(obs_properties_t *props, obs_property_t *p,
  1144. obs_data_t *settings)
  1145. {
  1146. int val = (int)obs_data_get_int(settings, RES_TYPE);
  1147. bool enabled = (val != ResType_Preferred);
  1148. p = obs_properties_get(props, RESOLUTION);
  1149. obs_property_set_enabled(p, enabled);
  1150. p = obs_properties_get(props, FRAME_INTERVAL);
  1151. obs_property_set_enabled(p, enabled);
  1152. p = obs_properties_get(props, VIDEO_FORMAT);
  1153. obs_property_set_enabled(p, enabled);
  1154. if (val == ResType_Custom) {
  1155. p = obs_properties_get(props, RESOLUTION);
  1156. DeviceResolutionChanged(props, p, settings);
  1157. } else {
  1158. obs_data_unset_autoselect_value(settings, FRAME_INTERVAL);
  1159. }
  1160. return true;
  1161. }
  1162. static DStr GetFPSName(long long interval)
  1163. {
  1164. DStr name;
  1165. if (interval == FPS_MATCHING) {
  1166. dstr_cat(name, TEXT_FPS_MATCHING);
  1167. return name;
  1168. }
  1169. if (interval == FPS_HIGHEST) {
  1170. dstr_cat(name, TEXT_FPS_HIGHEST);
  1171. return name;
  1172. }
  1173. for (const FPSFormat &format : validFPSFormats) {
  1174. if (format.interval != interval)
  1175. continue;
  1176. dstr_cat(name, format.text);
  1177. return name;
  1178. }
  1179. dstr_cat(name, to_string(10000000. / interval).c_str());
  1180. return name;
  1181. }
  1182. static void UpdateFPS(VideoDevice &device, VideoFormat format,
  1183. long long interval, int cx, int cy, obs_properties_t *props)
  1184. {
  1185. obs_property_t *list = obs_properties_get(props, FRAME_INTERVAL);
  1186. obs_property_list_clear(list);
  1187. obs_property_list_add_int(list, TEXT_FPS_MATCHING, FPS_MATCHING);
  1188. obs_property_list_add_int(list, TEXT_FPS_HIGHEST, FPS_HIGHEST);
  1189. bool interval_added = interval == FPS_HIGHEST ||
  1190. interval == FPS_MATCHING;
  1191. for (const FPSFormat &fps_format : validFPSFormats) {
  1192. bool video_format_match = false;
  1193. long long format_interval = fps_format.interval;
  1194. bool available = CapsMatch(device,
  1195. ResolutionMatcher(cx, cy),
  1196. VideoFormatMatcher(format, video_format_match),
  1197. FrameRateMatcher(format_interval));
  1198. if (!available && interval != fps_format.interval)
  1199. continue;
  1200. if (interval == fps_format.interval)
  1201. interval_added = true;
  1202. size_t idx = obs_property_list_add_int(list, fps_format.text,
  1203. fps_format.interval);
  1204. obs_property_list_item_disable(list, idx, !available);
  1205. }
  1206. if (interval_added)
  1207. return;
  1208. size_t idx = obs_property_list_add_int(list, GetFPSName(interval),
  1209. interval);
  1210. obs_property_list_item_disable(list, idx, true);
  1211. }
  1212. static DStr GetVideoFormatName(VideoFormat format)
  1213. {
  1214. DStr name;
  1215. for (const VideoFormatName &format_ : videoFormatNames) {
  1216. if (format_.format == format) {
  1217. dstr_cat(name, obs_module_text(format_.name));
  1218. return name;
  1219. }
  1220. }
  1221. dstr_cat(name, TEXT_FORMAT_UNKNOWN);
  1222. dstr_replace(name, "%1", std::to_string((long long)format).c_str());
  1223. return name;
  1224. }
  1225. static void UpdateVideoFormats(VideoDevice &device, VideoFormat format_,
  1226. int cx, int cy, long long interval, obs_properties_t *props)
  1227. {
  1228. set<VideoFormat> formats = { VideoFormat::Any };
  1229. auto format_gatherer = [&formats](const VideoInfo &info) mutable -> bool
  1230. {
  1231. formats.insert(info.format);
  1232. return false;
  1233. };
  1234. CapsMatch(device,
  1235. ResolutionMatcher(cx, cy),
  1236. FrameRateMatcher(interval),
  1237. format_gatherer);
  1238. obs_property_t *list = obs_properties_get(props, VIDEO_FORMAT);
  1239. obs_property_list_clear(list);
  1240. bool format_added = false;
  1241. for (const VideoFormatName &format : videoFormatNames) {
  1242. bool available = formats.find(format.format) != end(formats);
  1243. if (!available && format.format != format_)
  1244. continue;
  1245. if (format.format == format_)
  1246. format_added = true;
  1247. size_t idx = obs_property_list_add_int(list,
  1248. obs_module_text(format.name),
  1249. (long long)format.format);
  1250. obs_property_list_item_disable(list, idx, !available);
  1251. }
  1252. if (format_added)
  1253. return;
  1254. size_t idx = obs_property_list_add_int(list,
  1255. GetVideoFormatName(format_), (long long)format_);
  1256. obs_property_list_item_disable(list, idx, true);
  1257. }
  1258. static bool UpdateFPS(long long interval, obs_property_t *list)
  1259. {
  1260. size_t size = obs_property_list_item_count(list);
  1261. DStr name;
  1262. for (size_t i = 0; i < size; i++) {
  1263. if (obs_property_list_item_int(list, i) != interval)
  1264. continue;
  1265. obs_property_list_item_disable(list, i, true);
  1266. if (size == 1)
  1267. return false;
  1268. dstr_cat(name, obs_property_list_item_name(list, i));
  1269. break;
  1270. }
  1271. obs_property_list_clear(list);
  1272. if (!name->len)
  1273. name = GetFPSName(interval);
  1274. obs_property_list_add_int(list, name, interval);
  1275. obs_property_list_item_disable(list, 0, true);
  1276. return true;
  1277. }
  1278. static bool DeviceIntervalChanged(obs_properties_t *props, obs_property_t *p,
  1279. obs_data_t *settings)
  1280. {
  1281. long long val = obs_data_get_int(settings, FRAME_INTERVAL);
  1282. PropertiesData *data = (PropertiesData*)obs_properties_get_param(props);
  1283. const char *id = obs_data_get_string(settings, VIDEO_DEVICE_ID);
  1284. VideoDevice device;
  1285. if (!data->GetDevice(device, id))
  1286. return UpdateFPS(val, p);
  1287. int cx = 0, cy = 0;
  1288. if (!DetermineResolution(cx, cy, settings, device)) {
  1289. UpdateVideoFormats(device, VideoFormat::Any, 0, 0, 0, props);
  1290. UpdateFPS(device, VideoFormat::Any, 0, 0, 0, props);
  1291. return true;
  1292. }
  1293. int resType = (int)obs_data_get_int(settings, RES_TYPE);
  1294. if (resType != ResType_Custom)
  1295. return true;
  1296. if (val == FPS_MATCHING)
  1297. val = GetOBSFPS();
  1298. VideoFormat format = (VideoFormat)obs_data_get_int(settings,
  1299. VIDEO_FORMAT);
  1300. bool video_format_matches = false;
  1301. long long best_interval = numeric_limits<long long>::max();
  1302. bool frameRateSupported = CapsMatch(device,
  1303. ResolutionMatcher(cx, cy),
  1304. VideoFormatMatcher(format, video_format_matches),
  1305. ClosestFrameRateSelector(val, best_interval),
  1306. FrameRateMatcher(val));
  1307. if (video_format_matches &&
  1308. !frameRateSupported &&
  1309. best_interval != val) {
  1310. long long listed_val = 0;
  1311. for (const FPSFormat &format : validFPSFormats) {
  1312. long long diff = llabs(format.interval - best_interval);
  1313. if (diff < DEVICE_INTERVAL_DIFF_LIMIT) {
  1314. listed_val = format.interval;
  1315. break;
  1316. }
  1317. }
  1318. if (listed_val != val) {
  1319. obs_data_set_autoselect_int(settings, FRAME_INTERVAL,
  1320. listed_val);
  1321. val = listed_val;
  1322. }
  1323. } else {
  1324. obs_data_unset_autoselect_value(settings, FRAME_INTERVAL);
  1325. }
  1326. UpdateVideoFormats(device, format, cx, cy, val, props);
  1327. UpdateFPS(device, format, val, cx, cy, props);
  1328. UNUSED_PARAMETER(p);
  1329. return true;
  1330. }
  1331. static bool UpdateVideoFormats(VideoFormat format, obs_property_t *list)
  1332. {
  1333. size_t size = obs_property_list_item_count(list);
  1334. DStr name;
  1335. for (size_t i = 0; i < size; i++) {
  1336. if ((VideoFormat)obs_property_list_item_int(list, i) != format)
  1337. continue;
  1338. if (size == 1)
  1339. return false;
  1340. dstr_cat(name, obs_property_list_item_name(list, i));
  1341. break;
  1342. }
  1343. obs_property_list_clear(list);
  1344. if (!name->len)
  1345. name = GetVideoFormatName(format);
  1346. obs_property_list_add_int(list, name, (long long)format);
  1347. obs_property_list_item_disable(list, 0, true);
  1348. return true;
  1349. }
  1350. static bool VideoFormatChanged(obs_properties_t *props, obs_property_t *p,
  1351. obs_data_t *settings)
  1352. {
  1353. PropertiesData *data = (PropertiesData*)obs_properties_get_param(props);
  1354. const char *id = obs_data_get_string(settings, VIDEO_DEVICE_ID);
  1355. VideoDevice device;
  1356. VideoFormat curFormat =
  1357. (VideoFormat)obs_data_get_int(settings, VIDEO_FORMAT);
  1358. if (!data->GetDevice(device, id))
  1359. return UpdateVideoFormats(curFormat, p);
  1360. int cx, cy;
  1361. if (!DetermineResolution(cx, cy, settings, device)) {
  1362. UpdateVideoFormats(device, VideoFormat::Any, cx, cy, 0, props);
  1363. UpdateFPS(device, VideoFormat::Any, 0, 0, 0, props);
  1364. return true;
  1365. }
  1366. long long interval = obs_data_get_int(settings, FRAME_INTERVAL);
  1367. UpdateVideoFormats(device, curFormat, cx, cy, interval, props);
  1368. UpdateFPS(device, curFormat, interval, cx, cy, props);
  1369. return true;
  1370. }
  1371. static bool CustomAudioClicked(obs_properties_t *props, obs_property_t *p,
  1372. obs_data_t *settings)
  1373. {
  1374. bool useCustomAudio = obs_data_get_bool(settings, USE_CUSTOM_AUDIO);
  1375. p = obs_properties_get(props, AUDIO_DEVICE_ID);
  1376. obs_property_set_visible(p, useCustomAudio);
  1377. return true;
  1378. }
  1379. static bool ActivateClicked(obs_properties_t *, obs_property_t *p,
  1380. void *data)
  1381. {
  1382. DShowInput *input = reinterpret_cast<DShowInput*>(data);
  1383. if (input->active) {
  1384. input->SetActive(false);
  1385. obs_property_set_description(p, TEXT_ACTIVATE);
  1386. } else {
  1387. input->SetActive(true);
  1388. obs_property_set_description(p, TEXT_DEACTIVATE);
  1389. }
  1390. return true;
  1391. }
  1392. static obs_properties_t *GetDShowProperties(void *obj)
  1393. {
  1394. DShowInput *input = reinterpret_cast<DShowInput*>(obj);
  1395. obs_properties_t *ppts = obs_properties_create();
  1396. PropertiesData *data = new PropertiesData;
  1397. data->input = input;
  1398. obs_properties_set_param(ppts, data, PropertiesDataDestroy);
  1399. obs_property_t *p = obs_properties_add_list(ppts,
  1400. VIDEO_DEVICE_ID, TEXT_DEVICE,
  1401. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  1402. obs_property_set_modified_callback(p, DeviceSelectionChanged);
  1403. Device::EnumVideoDevices(data->devices);
  1404. for (const VideoDevice &device : data->devices)
  1405. AddDevice(p, device);
  1406. const char *activateText = TEXT_ACTIVATE;
  1407. if (input) {
  1408. if (input->active)
  1409. activateText = TEXT_DEACTIVATE;
  1410. }
  1411. obs_properties_add_button(ppts, "activate", activateText,
  1412. ActivateClicked);
  1413. obs_properties_add_button(ppts, "video_config", TEXT_CONFIG_VIDEO,
  1414. VideoConfigClicked);
  1415. obs_properties_add_button(ppts, "xbar_config", TEXT_CONFIG_XBAR,
  1416. CrossbarConfigClicked);
  1417. obs_properties_add_bool(ppts, DEACTIVATE_WNS, TEXT_DWNS);
  1418. /* ------------------------------------- */
  1419. /* video settings */
  1420. p = obs_properties_add_list(ppts, RES_TYPE, TEXT_RES_FPS_TYPE,
  1421. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  1422. obs_property_set_modified_callback(p, ResTypeChanged);
  1423. obs_property_list_add_int(p, TEXT_PREFERRED_RES, ResType_Preferred);
  1424. obs_property_list_add_int(p, TEXT_CUSTOM_RES, ResType_Custom);
  1425. p = obs_properties_add_list(ppts, RESOLUTION, TEXT_RESOLUTION,
  1426. OBS_COMBO_TYPE_EDITABLE, OBS_COMBO_FORMAT_STRING);
  1427. obs_property_set_modified_callback(p, DeviceResolutionChanged);
  1428. p = obs_properties_add_list(ppts, FRAME_INTERVAL, "FPS",
  1429. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  1430. obs_property_set_modified_callback(p, DeviceIntervalChanged);
  1431. p = obs_properties_add_list(ppts, VIDEO_FORMAT, TEXT_VIDEO_FORMAT,
  1432. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  1433. obs_property_set_modified_callback(p, VideoFormatChanged);
  1434. p = obs_properties_add_list(ppts, COLOR_SPACE, TEXT_COLOR_SPACE,
  1435. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  1436. obs_property_list_add_string(p, TEXT_COLOR_DEFAULT, "default");
  1437. obs_property_list_add_string(p, "709", "709");
  1438. obs_property_list_add_string(p, "601", "601");
  1439. p = obs_properties_add_list(ppts, COLOR_RANGE, TEXT_COLOR_RANGE,
  1440. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  1441. obs_property_list_add_string(p, TEXT_RANGE_PARTIAL, "partial");
  1442. obs_property_list_add_string(p, TEXT_RANGE_FULL, "full");
  1443. p = obs_properties_add_list(ppts, BUFFERING_VAL, TEXT_BUFFERING,
  1444. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  1445. obs_property_list_add_int(p, TEXT_BUFFERING_AUTO,
  1446. (int64_t)BufferingType::Auto);
  1447. obs_property_list_add_int(p, TEXT_BUFFERING_ON,
  1448. (int64_t)BufferingType::On);
  1449. obs_property_list_add_int(p, TEXT_BUFFERING_OFF,
  1450. (int64_t)BufferingType::Off);
  1451. obs_property_set_long_description(p,
  1452. obs_module_text("Buffering.ToolTip"));
  1453. obs_properties_add_bool(ppts, FLIP_IMAGE, TEXT_FLIP_IMAGE);
  1454. /* ------------------------------------- */
  1455. /* audio settings */
  1456. Device::EnumAudioDevices(data->audioDevices);
  1457. p = obs_properties_add_list(ppts, AUDIO_OUTPUT_MODE, TEXT_AUDIO_MODE,
  1458. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  1459. obs_property_list_add_int(p, TEXT_MODE_CAPTURE,
  1460. (int64_t)AudioMode::Capture);
  1461. obs_property_list_add_int(p, TEXT_MODE_DSOUND,
  1462. (int64_t)AudioMode::DirectSound);
  1463. obs_property_list_add_int(p, TEXT_MODE_WAVEOUT,
  1464. (int64_t)AudioMode::WaveOut);
  1465. if (!data->audioDevices.size())
  1466. return ppts;
  1467. p = obs_properties_add_bool(ppts, USE_CUSTOM_AUDIO, TEXT_CUSTOM_AUDIO);
  1468. obs_property_set_modified_callback(p, CustomAudioClicked);
  1469. p = obs_properties_add_list(ppts, AUDIO_DEVICE_ID, TEXT_AUDIO_DEVICE,
  1470. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  1471. for (const AudioDevice &device : data->audioDevices)
  1472. AddAudioDevice(p, device);
  1473. return ppts;
  1474. }
  1475. void DShowModuleLogCallback(LogType type, const wchar_t *msg, void *param)
  1476. {
  1477. int obs_type = LOG_DEBUG;
  1478. switch (type) {
  1479. case LogType::Error: obs_type = LOG_ERROR; break;
  1480. case LogType::Warning: obs_type = LOG_WARNING; break;
  1481. case LogType::Info: obs_type = LOG_INFO; break;
  1482. case LogType::Debug: obs_type = LOG_DEBUG; break;
  1483. }
  1484. DStr dmsg;
  1485. dstr_from_wcs(dmsg, msg);
  1486. blog(obs_type, "DShow: %s", dmsg->array);
  1487. UNUSED_PARAMETER(param);
  1488. }
  1489. static void HideDShowInput(void *data)
  1490. {
  1491. DShowInput *input = reinterpret_cast<DShowInput*>(data);
  1492. if (input->deactivateWhenNotShowing && input->active)
  1493. input->QueueAction(Action::Deactivate);
  1494. }
  1495. static void ShowDShowInput(void *data)
  1496. {
  1497. DShowInput *input = reinterpret_cast<DShowInput*>(data);
  1498. if (input->deactivateWhenNotShowing && input->active)
  1499. input->QueueAction(Action::Activate);
  1500. }
  1501. void RegisterDShowSource()
  1502. {
  1503. SetLogCallback(DShowModuleLogCallback, nullptr);
  1504. obs_source_info info = {};
  1505. info.id = "dshow_input";
  1506. info.type = OBS_SOURCE_TYPE_INPUT;
  1507. info.output_flags = OBS_SOURCE_VIDEO |
  1508. OBS_SOURCE_AUDIO |
  1509. OBS_SOURCE_ASYNC |
  1510. OBS_SOURCE_DO_NOT_DUPLICATE;
  1511. info.show = ShowDShowInput;
  1512. info.hide = HideDShowInput;
  1513. info.get_name = GetDShowInputName;
  1514. info.create = CreateDShowInput;
  1515. info.destroy = DestroyDShowInput;
  1516. info.update = UpdateDShowInput;
  1517. info.get_defaults = GetDShowDefaults;
  1518. info.get_properties = GetDShowProperties;
  1519. obs_register_source(&info);
  1520. }