win-dshow.cpp 50 KB

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