win-dshow.cpp 51 KB

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