win-dshow.cpp 52 KB

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