win-dshow.cpp 51 KB

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