win-dshow.cpp 51 KB

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