1
0

win-dshow.cpp 52 KB

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