win-dshow.cpp 49 KB

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