win-dshow.cpp 52 KB

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