win-dshow.cpp 52 KB

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