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