win-dshow.cpp 50 KB

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