win-wasapi.cpp 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747
  1. #include "wasapi-notify.hpp"
  2. #include "enum-wasapi.hpp"
  3. #include <obs-module.h>
  4. #include <obs.h>
  5. #include <util/dstr.h>
  6. #include <util/platform.h>
  7. #include <util/windows/HRError.hpp>
  8. #include <util/windows/ComPtr.hpp>
  9. #include <util/windows/WinHandle.hpp>
  10. #include <util/windows/CoTaskMemPtr.hpp>
  11. #include <util/windows/win-version.h>
  12. #include <util/windows/window-helpers.h>
  13. #include <util/threading.h>
  14. #include <util/util_uint64.h>
  15. #include <atomic>
  16. #include <cinttypes>
  17. #include <audioclientactivationparams.h>
  18. #include <avrt.h>
  19. #include <RTWorkQ.h>
  20. #include <wrl/implements.h>
  21. using namespace std;
  22. #define OPT_DEVICE_ID "device_id"
  23. #define OPT_USE_DEVICE_TIMING "use_device_timing"
  24. #define OPT_WINDOW "window"
  25. #define OPT_PRIORITY "priority"
  26. WASAPINotify *GetNotify();
  27. static void GetWASAPIDefaults(obs_data_t *settings);
  28. #define OBS_KSAUDIO_SPEAKER_4POINT1 \
  29. (KSAUDIO_SPEAKER_SURROUND | SPEAKER_LOW_FREQUENCY)
  30. typedef HRESULT(STDAPICALLTYPE *PFN_ActivateAudioInterfaceAsync)(
  31. LPCWSTR, REFIID, PROPVARIANT *,
  32. IActivateAudioInterfaceCompletionHandler *,
  33. IActivateAudioInterfaceAsyncOperation **);
  34. typedef HRESULT(STDAPICALLTYPE *PFN_RtwqUnlockWorkQueue)(DWORD);
  35. typedef HRESULT(STDAPICALLTYPE *PFN_RtwqLockSharedWorkQueue)(PCWSTR usageClass,
  36. LONG basePriority,
  37. DWORD *taskId,
  38. DWORD *id);
  39. typedef HRESULT(STDAPICALLTYPE *PFN_RtwqCreateAsyncResult)(IUnknown *,
  40. IRtwqAsyncCallback *,
  41. IUnknown *,
  42. IRtwqAsyncResult **);
  43. typedef HRESULT(STDAPICALLTYPE *PFN_RtwqPutWorkItem)(DWORD, LONG,
  44. IRtwqAsyncResult *);
  45. typedef HRESULT(STDAPICALLTYPE *PFN_RtwqPutWaitingWorkItem)(HANDLE, LONG,
  46. IRtwqAsyncResult *,
  47. RTWQWORKITEM_KEY *);
  48. class WASAPIActivateAudioInterfaceCompletionHandler
  49. : public Microsoft::WRL::RuntimeClass<
  50. Microsoft::WRL::RuntimeClassFlags<Microsoft::WRL::ClassicCom>,
  51. Microsoft::WRL::FtmBase,
  52. IActivateAudioInterfaceCompletionHandler> {
  53. IUnknown *unknown;
  54. HRESULT activationResult;
  55. WinHandle activationSignal;
  56. public:
  57. WASAPIActivateAudioInterfaceCompletionHandler();
  58. HRESULT GetActivateResult(IAudioClient **client);
  59. private:
  60. virtual HRESULT STDMETHODCALLTYPE ActivateCompleted(
  61. IActivateAudioInterfaceAsyncOperation *activateOperation)
  62. override final;
  63. };
  64. WASAPIActivateAudioInterfaceCompletionHandler::
  65. WASAPIActivateAudioInterfaceCompletionHandler()
  66. {
  67. activationSignal = CreateEvent(nullptr, false, false, nullptr);
  68. if (!activationSignal.Valid())
  69. throw "Could not create receive signal";
  70. }
  71. HRESULT
  72. WASAPIActivateAudioInterfaceCompletionHandler::GetActivateResult(
  73. IAudioClient **client)
  74. {
  75. WaitForSingleObject(activationSignal, INFINITE);
  76. *client = static_cast<IAudioClient *>(unknown);
  77. return activationResult;
  78. }
  79. HRESULT
  80. WASAPIActivateAudioInterfaceCompletionHandler::ActivateCompleted(
  81. IActivateAudioInterfaceAsyncOperation *activateOperation)
  82. {
  83. HRESULT hr, hr_activate;
  84. hr = activateOperation->GetActivateResult(&hr_activate, &unknown);
  85. hr = SUCCEEDED(hr) ? hr_activate : hr;
  86. activationResult = hr;
  87. SetEvent(activationSignal);
  88. return hr;
  89. }
  90. enum class SourceType {
  91. Input,
  92. DeviceOutput,
  93. ProcessOutput,
  94. };
  95. class ARtwqAsyncCallback : public IRtwqAsyncCallback {
  96. protected:
  97. ARtwqAsyncCallback(void *source) : source(source) {}
  98. public:
  99. STDMETHOD_(ULONG, AddRef)() { return ++refCount; }
  100. STDMETHOD_(ULONG, Release)() { return --refCount; }
  101. STDMETHOD(QueryInterface)(REFIID riid, void **ppvObject)
  102. {
  103. HRESULT hr = E_NOINTERFACE;
  104. if (riid == __uuidof(IRtwqAsyncCallback) ||
  105. riid == __uuidof(IUnknown)) {
  106. *ppvObject = this;
  107. AddRef();
  108. hr = S_OK;
  109. } else {
  110. *ppvObject = NULL;
  111. }
  112. return hr;
  113. }
  114. STDMETHOD(GetParameters)
  115. (DWORD *pdwFlags, DWORD *pdwQueue)
  116. {
  117. *pdwFlags = 0;
  118. *pdwQueue = queue_id;
  119. return S_OK;
  120. }
  121. STDMETHOD(Invoke)
  122. (IRtwqAsyncResult *) override = 0;
  123. DWORD GetQueueId() const { return queue_id; }
  124. void SetQueueId(DWORD id) { queue_id = id; }
  125. protected:
  126. std::atomic<ULONG> refCount = 1;
  127. void *source;
  128. DWORD queue_id = 0;
  129. };
  130. class WASAPISource {
  131. ComPtr<IMMDeviceEnumerator> enumerator;
  132. ComPtr<IAudioClient> client;
  133. ComPtr<IAudioCaptureClient> capture;
  134. obs_source_t *source;
  135. wstring default_id;
  136. string device_id;
  137. string device_name;
  138. WinModule mmdevapi_module;
  139. PFN_ActivateAudioInterfaceAsync activate_audio_interface_async = NULL;
  140. PFN_RtwqUnlockWorkQueue rtwq_unlock_work_queue = NULL;
  141. PFN_RtwqLockSharedWorkQueue rtwq_lock_shared_work_queue = NULL;
  142. PFN_RtwqCreateAsyncResult rtwq_create_async_result = NULL;
  143. PFN_RtwqPutWorkItem rtwq_put_work_item = NULL;
  144. PFN_RtwqPutWaitingWorkItem rtwq_put_waiting_work_item = NULL;
  145. bool rtwq_supported = false;
  146. window_priority priority;
  147. string window_class;
  148. string title;
  149. string executable;
  150. HWND hwnd = NULL;
  151. DWORD process_id = 0;
  152. const SourceType sourceType;
  153. std::atomic<bool> useDeviceTiming = false;
  154. std::atomic<bool> isDefaultDevice = false;
  155. bool hooked = false;
  156. bool previouslyFailed = false;
  157. WinHandle reconnectThread = NULL;
  158. class CallbackStartCapture : public ARtwqAsyncCallback {
  159. public:
  160. CallbackStartCapture(WASAPISource *source)
  161. : ARtwqAsyncCallback(source)
  162. {
  163. }
  164. STDMETHOD(Invoke)
  165. (IRtwqAsyncResult *) override
  166. {
  167. ((WASAPISource *)source)->OnStartCapture();
  168. return S_OK;
  169. }
  170. } startCapture;
  171. ComPtr<IRtwqAsyncResult> startCaptureAsyncResult;
  172. class CallbackSampleReady : public ARtwqAsyncCallback {
  173. public:
  174. CallbackSampleReady(WASAPISource *source)
  175. : ARtwqAsyncCallback(source)
  176. {
  177. }
  178. STDMETHOD(Invoke)
  179. (IRtwqAsyncResult *) override
  180. {
  181. ((WASAPISource *)source)->OnSampleReady();
  182. return S_OK;
  183. }
  184. } sampleReady;
  185. ComPtr<IRtwqAsyncResult> sampleReadyAsyncResult;
  186. class CallbackRestart : public ARtwqAsyncCallback {
  187. public:
  188. CallbackRestart(WASAPISource *source)
  189. : ARtwqAsyncCallback(source)
  190. {
  191. }
  192. STDMETHOD(Invoke)
  193. (IRtwqAsyncResult *) override
  194. {
  195. ((WASAPISource *)source)->OnRestart();
  196. return S_OK;
  197. }
  198. } restart;
  199. ComPtr<IRtwqAsyncResult> restartAsyncResult;
  200. WinHandle captureThread;
  201. WinHandle idleSignal;
  202. WinHandle stopSignal;
  203. WinHandle receiveSignal;
  204. WinHandle restartSignal;
  205. WinHandle reconnectExitSignal;
  206. WinHandle exitSignal;
  207. WinHandle initSignal;
  208. DWORD reconnectDuration = 0;
  209. WinHandle reconnectSignal;
  210. speaker_layout speakers;
  211. audio_format format;
  212. uint32_t sampleRate;
  213. vector<BYTE> silence;
  214. static DWORD WINAPI ReconnectThread(LPVOID param);
  215. static DWORD WINAPI CaptureThread(LPVOID param);
  216. bool ProcessCaptureData();
  217. void Start();
  218. void Stop();
  219. static ComPtr<IMMDevice> InitDevice(IMMDeviceEnumerator *enumerator,
  220. bool isDefaultDevice,
  221. SourceType type,
  222. const string device_id);
  223. static ComPtr<IAudioClient> InitClient(
  224. IMMDevice *device, SourceType type, DWORD process_id,
  225. PFN_ActivateAudioInterfaceAsync activate_audio_interface_async,
  226. speaker_layout &speakers, audio_format &format,
  227. uint32_t &sampleRate);
  228. static void InitFormat(const WAVEFORMATEX *wfex,
  229. enum speaker_layout &speakers,
  230. enum audio_format &format, uint32_t &sampleRate);
  231. static void ClearBuffer(IMMDevice *device);
  232. static ComPtr<IAudioCaptureClient> InitCapture(IAudioClient *client,
  233. HANDLE receiveSignal);
  234. void Initialize();
  235. bool TryInitialize();
  236. struct UpdateParams {
  237. string device_id;
  238. bool useDeviceTiming;
  239. bool isDefaultDevice;
  240. window_priority priority;
  241. string window_class;
  242. string title;
  243. string executable;
  244. };
  245. UpdateParams BuildUpdateParams(obs_data_t *settings);
  246. void UpdateSettings(UpdateParams &&params);
  247. void LogSettings();
  248. public:
  249. WASAPISource(obs_data_t *settings, obs_source_t *source_,
  250. SourceType type);
  251. ~WASAPISource();
  252. void Update(obs_data_t *settings);
  253. void OnWindowChanged(obs_data_t *settings);
  254. void Activate();
  255. void Deactivate();
  256. void SetDefaultDevice(EDataFlow flow, ERole role, LPCWSTR id);
  257. void OnStartCapture();
  258. void OnSampleReady();
  259. void OnRestart();
  260. bool GetHooked();
  261. HWND GetHwnd();
  262. };
  263. WASAPISource::WASAPISource(obs_data_t *settings, obs_source_t *source_,
  264. SourceType type)
  265. : source(source_),
  266. sourceType(type),
  267. startCapture(this),
  268. sampleReady(this),
  269. restart(this)
  270. {
  271. mmdevapi_module = LoadLibrary(L"Mmdevapi");
  272. if (mmdevapi_module) {
  273. activate_audio_interface_async =
  274. (PFN_ActivateAudioInterfaceAsync)GetProcAddress(
  275. mmdevapi_module, "ActivateAudioInterfaceAsync");
  276. }
  277. UpdateSettings(BuildUpdateParams(settings));
  278. LogSettings();
  279. idleSignal = CreateEvent(nullptr, true, false, nullptr);
  280. if (!idleSignal.Valid())
  281. throw "Could not create idle signal";
  282. stopSignal = CreateEvent(nullptr, true, false, nullptr);
  283. if (!stopSignal.Valid())
  284. throw "Could not create stop signal";
  285. receiveSignal = CreateEvent(nullptr, false, false, nullptr);
  286. if (!receiveSignal.Valid())
  287. throw "Could not create receive signal";
  288. restartSignal = CreateEvent(nullptr, true, false, nullptr);
  289. if (!restartSignal.Valid())
  290. throw "Could not create restart signal";
  291. reconnectExitSignal = CreateEvent(nullptr, true, false, nullptr);
  292. if (!reconnectExitSignal.Valid())
  293. throw "Could not create reconnect exit signal";
  294. exitSignal = CreateEvent(nullptr, true, false, nullptr);
  295. if (!exitSignal.Valid())
  296. throw "Could not create exit signal";
  297. initSignal = CreateEvent(nullptr, false, false, nullptr);
  298. if (!initSignal.Valid())
  299. throw "Could not create init signal";
  300. reconnectSignal = CreateEvent(nullptr, false, false, nullptr);
  301. if (!reconnectSignal.Valid())
  302. throw "Could not create reconnect signal";
  303. HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr,
  304. CLSCTX_ALL,
  305. IID_PPV_ARGS(enumerator.Assign()));
  306. if (FAILED(hr))
  307. throw HRError("Failed to create enumerator", hr);
  308. /* OBS will already load DLL on startup if it exists */
  309. const HMODULE rtwq_module = GetModuleHandle(L"RTWorkQ.dll");
  310. // while RTWQ was introduced in Win 8.1, it silently fails
  311. // to capture Desktop Audio for some reason. Disable for now.
  312. struct win_version_info win1703 = {};
  313. win1703.major = 10;
  314. win1703.minor = 0;
  315. win1703.build = 15063;
  316. win1703.revis = 0;
  317. struct win_version_info ver;
  318. get_win_ver(&ver);
  319. if (win_version_compare(&ver, &win1703) >= 0)
  320. rtwq_supported = rtwq_module != NULL;
  321. if (rtwq_supported) {
  322. rtwq_unlock_work_queue =
  323. (PFN_RtwqUnlockWorkQueue)GetProcAddress(
  324. rtwq_module, "RtwqUnlockWorkQueue");
  325. rtwq_lock_shared_work_queue =
  326. (PFN_RtwqLockSharedWorkQueue)GetProcAddress(
  327. rtwq_module, "RtwqLockSharedWorkQueue");
  328. rtwq_create_async_result =
  329. (PFN_RtwqCreateAsyncResult)GetProcAddress(
  330. rtwq_module, "RtwqCreateAsyncResult");
  331. rtwq_put_work_item = (PFN_RtwqPutWorkItem)GetProcAddress(
  332. rtwq_module, "RtwqPutWorkItem");
  333. rtwq_put_waiting_work_item =
  334. (PFN_RtwqPutWaitingWorkItem)GetProcAddress(
  335. rtwq_module, "RtwqPutWaitingWorkItem");
  336. try {
  337. hr = rtwq_create_async_result(nullptr, &startCapture,
  338. nullptr,
  339. &startCaptureAsyncResult);
  340. if (FAILED(hr)) {
  341. throw HRError(
  342. "Could not create startCaptureAsyncResult",
  343. hr);
  344. }
  345. hr = rtwq_create_async_result(nullptr, &sampleReady,
  346. nullptr,
  347. &sampleReadyAsyncResult);
  348. if (FAILED(hr)) {
  349. throw HRError(
  350. "Could not create sampleReadyAsyncResult",
  351. hr);
  352. }
  353. hr = rtwq_create_async_result(nullptr, &restart,
  354. nullptr,
  355. &restartAsyncResult);
  356. if (FAILED(hr)) {
  357. throw HRError(
  358. "Could not create restartAsyncResult",
  359. hr);
  360. }
  361. DWORD taskId = 0;
  362. DWORD id = 0;
  363. hr = rtwq_lock_shared_work_queue(L"Capture", 0, &taskId,
  364. &id);
  365. if (FAILED(hr)) {
  366. throw HRError("RtwqLockSharedWorkQueue failed",
  367. hr);
  368. }
  369. startCapture.SetQueueId(id);
  370. sampleReady.SetQueueId(id);
  371. restart.SetQueueId(id);
  372. } catch (HRError &err) {
  373. blog(LOG_ERROR, "RTWQ setup failed: %s (0x%08X)",
  374. err.str, err.hr);
  375. rtwq_supported = false;
  376. }
  377. }
  378. if (!rtwq_supported) {
  379. captureThread = CreateThread(nullptr, 0,
  380. WASAPISource::CaptureThread, this,
  381. 0, nullptr);
  382. if (!captureThread.Valid()) {
  383. throw "Failed to create capture thread";
  384. }
  385. }
  386. auto notify = GetNotify();
  387. if (notify) {
  388. notify->AddDefaultDeviceChangedCallback(
  389. this,
  390. std::bind(&WASAPISource::SetDefaultDevice, this,
  391. std::placeholders::_1, std::placeholders::_2,
  392. std::placeholders::_3));
  393. }
  394. Start();
  395. }
  396. void WASAPISource::Start()
  397. {
  398. if (rtwq_supported) {
  399. rtwq_put_work_item(startCapture.GetQueueId(), 0,
  400. startCaptureAsyncResult);
  401. } else {
  402. SetEvent(initSignal);
  403. }
  404. }
  405. void WASAPISource::Stop()
  406. {
  407. SetEvent(stopSignal);
  408. blog(LOG_INFO, "WASAPI: Device '%s' Terminated", device_name.c_str());
  409. if (rtwq_supported)
  410. SetEvent(receiveSignal);
  411. if (reconnectThread.Valid()) {
  412. WaitForSingleObject(idleSignal, INFINITE);
  413. } else {
  414. const HANDLE sigs[] = {reconnectSignal, idleSignal};
  415. WaitForMultipleObjects(_countof(sigs), sigs, false, INFINITE);
  416. }
  417. SetEvent(exitSignal);
  418. if (reconnectThread.Valid()) {
  419. SetEvent(reconnectExitSignal);
  420. WaitForSingleObject(reconnectThread, INFINITE);
  421. }
  422. if (rtwq_supported)
  423. rtwq_unlock_work_queue(sampleReady.GetQueueId());
  424. else
  425. WaitForSingleObject(captureThread, INFINITE);
  426. }
  427. WASAPISource::~WASAPISource()
  428. {
  429. auto notify = GetNotify();
  430. if (notify) {
  431. notify->RemoveDefaultDeviceChangedCallback(this);
  432. }
  433. Stop();
  434. }
  435. WASAPISource::UpdateParams WASAPISource::BuildUpdateParams(obs_data_t *settings)
  436. {
  437. WASAPISource::UpdateParams params;
  438. params.device_id = obs_data_get_string(settings, OPT_DEVICE_ID);
  439. params.useDeviceTiming =
  440. obs_data_get_bool(settings, OPT_USE_DEVICE_TIMING);
  441. params.isDefaultDevice =
  442. _strcmpi(params.device_id.c_str(), "default") == 0;
  443. params.priority =
  444. (window_priority)obs_data_get_int(settings, "priority");
  445. params.window_class.clear();
  446. params.title.clear();
  447. params.executable.clear();
  448. if (sourceType != SourceType::Input) {
  449. const char *const window =
  450. obs_data_get_string(settings, OPT_WINDOW);
  451. char *window_class = nullptr;
  452. char *title = nullptr;
  453. char *executable = nullptr;
  454. ms_build_window_strings(window, &window_class, &title,
  455. &executable);
  456. if (window_class) {
  457. params.window_class = window_class;
  458. bfree(window_class);
  459. }
  460. if (title) {
  461. params.title = title;
  462. bfree(title);
  463. }
  464. if (executable) {
  465. params.executable = executable;
  466. bfree(executable);
  467. }
  468. }
  469. return params;
  470. }
  471. void WASAPISource::UpdateSettings(UpdateParams &&params)
  472. {
  473. device_id = std::move(params.device_id);
  474. useDeviceTiming = params.useDeviceTiming;
  475. isDefaultDevice = params.isDefaultDevice;
  476. priority = params.priority;
  477. window_class = std::move(params.window_class);
  478. title = std::move(params.title);
  479. executable = std::move(params.executable);
  480. }
  481. void WASAPISource::LogSettings()
  482. {
  483. if (sourceType == SourceType::ProcessOutput) {
  484. blog(LOG_INFO,
  485. "[win-wasapi: '%s'] update settings:\n"
  486. "\texecutable: %s\n"
  487. "\ttitle: %s\n"
  488. "\tclass: %s\n"
  489. "\tpriority: %d",
  490. obs_source_get_name(source), executable.c_str(),
  491. title.c_str(), window_class.c_str(), (int)priority);
  492. } else {
  493. blog(LOG_INFO,
  494. "[win-wasapi: '%s'] update settings:\n"
  495. "\tdevice id: %s\n"
  496. "\tuse device timing: %d",
  497. obs_source_get_name(source), device_id.c_str(),
  498. (int)useDeviceTiming);
  499. }
  500. }
  501. void WASAPISource::Update(obs_data_t *settings)
  502. {
  503. UpdateParams params = BuildUpdateParams(settings);
  504. const bool restart =
  505. (sourceType == SourceType::ProcessOutput)
  506. ? ((priority != params.priority) ||
  507. (window_class != params.window_class) ||
  508. (title != params.title) ||
  509. (executable != params.executable))
  510. : (device_id.compare(params.device_id) != 0);
  511. UpdateSettings(std::move(params));
  512. LogSettings();
  513. if (restart)
  514. SetEvent(restartSignal);
  515. }
  516. void WASAPISource::OnWindowChanged(obs_data_t *settings)
  517. {
  518. UpdateParams params = BuildUpdateParams(settings);
  519. const bool restart =
  520. (sourceType == SourceType::ProcessOutput)
  521. ? ((priority != params.priority) ||
  522. (window_class != params.window_class) ||
  523. (title != params.title) ||
  524. (executable != params.executable))
  525. : (device_id.compare(params.device_id) != 0);
  526. UpdateSettings(std::move(params));
  527. if (restart)
  528. SetEvent(restartSignal);
  529. }
  530. void WASAPISource::Activate()
  531. {
  532. if (!reconnectThread.Valid()) {
  533. ResetEvent(reconnectExitSignal);
  534. reconnectThread = CreateThread(nullptr, 0,
  535. WASAPISource::ReconnectThread,
  536. this, 0, nullptr);
  537. }
  538. }
  539. void WASAPISource::Deactivate()
  540. {
  541. if (reconnectThread.Valid()) {
  542. SetEvent(reconnectExitSignal);
  543. WaitForSingleObject(reconnectThread, INFINITE);
  544. reconnectThread = NULL;
  545. }
  546. }
  547. ComPtr<IMMDevice> WASAPISource::InitDevice(IMMDeviceEnumerator *enumerator,
  548. bool isDefaultDevice,
  549. SourceType type,
  550. const string device_id)
  551. {
  552. ComPtr<IMMDevice> device;
  553. if (isDefaultDevice) {
  554. const bool input = type == SourceType::Input;
  555. HRESULT res = enumerator->GetDefaultAudioEndpoint(
  556. input ? eCapture : eRender,
  557. input ? eCommunications : eConsole, device.Assign());
  558. if (FAILED(res))
  559. throw HRError("Failed GetDefaultAudioEndpoint", res);
  560. } else {
  561. wchar_t *w_id;
  562. os_utf8_to_wcs_ptr(device_id.c_str(), device_id.size(), &w_id);
  563. if (!w_id)
  564. throw "Failed to widen device id string";
  565. const HRESULT res =
  566. enumerator->GetDevice(w_id, device.Assign());
  567. bfree(w_id);
  568. if (FAILED(res))
  569. throw HRError("Failed to enumerate device", res);
  570. }
  571. return device;
  572. }
  573. #define BUFFER_TIME_100NS (5 * 10000000)
  574. static DWORD GetSpeakerChannelMask(speaker_layout layout)
  575. {
  576. switch (layout) {
  577. case SPEAKERS_STEREO:
  578. return KSAUDIO_SPEAKER_STEREO;
  579. case SPEAKERS_2POINT1:
  580. return KSAUDIO_SPEAKER_2POINT1;
  581. case SPEAKERS_4POINT0:
  582. return KSAUDIO_SPEAKER_SURROUND;
  583. case SPEAKERS_4POINT1:
  584. return OBS_KSAUDIO_SPEAKER_4POINT1;
  585. case SPEAKERS_5POINT1:
  586. return KSAUDIO_SPEAKER_5POINT1_SURROUND;
  587. case SPEAKERS_7POINT1:
  588. return KSAUDIO_SPEAKER_7POINT1_SURROUND;
  589. }
  590. return (DWORD)layout;
  591. }
  592. ComPtr<IAudioClient> WASAPISource::InitClient(
  593. IMMDevice *device, SourceType type, DWORD process_id,
  594. PFN_ActivateAudioInterfaceAsync activate_audio_interface_async,
  595. speaker_layout &speakers, audio_format &format,
  596. uint32_t &samples_per_sec)
  597. {
  598. WAVEFORMATEXTENSIBLE wfextensible;
  599. CoTaskMemPtr<WAVEFORMATEX> wfex;
  600. const WAVEFORMATEX *pFormat;
  601. HRESULT res;
  602. ComPtr<IAudioClient> client;
  603. if (type == SourceType::ProcessOutput) {
  604. if (activate_audio_interface_async == NULL)
  605. throw "ActivateAudioInterfaceAsync is not available";
  606. struct obs_audio_info oai;
  607. obs_get_audio_info(&oai);
  608. const WORD nChannels = (WORD)get_audio_channels(oai.speakers);
  609. const DWORD nSamplesPerSec = oai.samples_per_sec;
  610. constexpr WORD wBitsPerSample = 32;
  611. const WORD nBlockAlign = nChannels * wBitsPerSample / 8;
  612. WAVEFORMATEX &wf = wfextensible.Format;
  613. wf.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
  614. wf.nChannels = nChannels;
  615. wf.nSamplesPerSec = nSamplesPerSec;
  616. wf.nAvgBytesPerSec = nSamplesPerSec * nBlockAlign;
  617. wf.nBlockAlign = nBlockAlign;
  618. wf.wBitsPerSample = wBitsPerSample;
  619. wf.cbSize = sizeof(wfextensible) - sizeof(wf);
  620. wfextensible.Samples.wValidBitsPerSample = wBitsPerSample;
  621. wfextensible.dwChannelMask =
  622. GetSpeakerChannelMask(oai.speakers);
  623. wfextensible.SubFormat = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
  624. AUDIOCLIENT_ACTIVATION_PARAMS audioclientActivationParams;
  625. audioclientActivationParams.ActivationType =
  626. AUDIOCLIENT_ACTIVATION_TYPE_PROCESS_LOOPBACK;
  627. audioclientActivationParams.ProcessLoopbackParams
  628. .TargetProcessId = process_id;
  629. audioclientActivationParams.ProcessLoopbackParams
  630. .ProcessLoopbackMode =
  631. PROCESS_LOOPBACK_MODE_INCLUDE_TARGET_PROCESS_TREE;
  632. PROPVARIANT activateParams{};
  633. activateParams.vt = VT_BLOB;
  634. activateParams.blob.cbSize =
  635. sizeof(audioclientActivationParams);
  636. activateParams.blob.pBlobData =
  637. reinterpret_cast<BYTE *>(&audioclientActivationParams);
  638. {
  639. Microsoft::WRL::ComPtr<
  640. WASAPIActivateAudioInterfaceCompletionHandler>
  641. handler = Microsoft::WRL::Make<
  642. WASAPIActivateAudioInterfaceCompletionHandler>();
  643. ComPtr<IActivateAudioInterfaceAsyncOperation> asyncOp;
  644. res = activate_audio_interface_async(
  645. VIRTUAL_AUDIO_DEVICE_PROCESS_LOOPBACK,
  646. __uuidof(IAudioClient), &activateParams,
  647. handler.Get(), &asyncOp);
  648. if (FAILED(res))
  649. throw HRError(
  650. "Failed to get activate audio client",
  651. res);
  652. res = handler->GetActivateResult(client.Assign());
  653. if (FAILED(res))
  654. throw HRError("Async activation failed", res);
  655. }
  656. pFormat = &wf;
  657. } else {
  658. res = device->Activate(__uuidof(IAudioClient), CLSCTX_ALL,
  659. nullptr, (void **)client.Assign());
  660. if (FAILED(res))
  661. throw HRError("Failed to activate client context", res);
  662. res = client->GetMixFormat(&wfex);
  663. if (FAILED(res))
  664. throw HRError("Failed to get mix format", res);
  665. pFormat = wfex.Get();
  666. }
  667. InitFormat(pFormat, speakers, format, samples_per_sec);
  668. DWORD flags = AUDCLNT_STREAMFLAGS_EVENTCALLBACK;
  669. if (type != SourceType::Input)
  670. flags |= AUDCLNT_STREAMFLAGS_LOOPBACK;
  671. res = client->Initialize(AUDCLNT_SHAREMODE_SHARED, flags,
  672. BUFFER_TIME_100NS, 0, pFormat, nullptr);
  673. if (FAILED(res))
  674. throw HRError("Failed to initialize audio client", res);
  675. return client;
  676. }
  677. void WASAPISource::ClearBuffer(IMMDevice *device)
  678. {
  679. CoTaskMemPtr<WAVEFORMATEX> wfex;
  680. HRESULT res;
  681. LPBYTE buffer;
  682. UINT32 frames;
  683. ComPtr<IAudioClient> client;
  684. res = device->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr,
  685. (void **)client.Assign());
  686. if (FAILED(res))
  687. throw HRError("Failed to activate client context", res);
  688. res = client->GetMixFormat(&wfex);
  689. if (FAILED(res))
  690. throw HRError("Failed to get mix format", res);
  691. res = client->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, BUFFER_TIME_100NS,
  692. 0, wfex, nullptr);
  693. if (FAILED(res))
  694. throw HRError("Failed to initialize audio client", res);
  695. /* Silent loopback fix. Prevents audio stream from stopping and */
  696. /* messing up timestamps and other weird glitches during silence */
  697. /* by playing a silent sample all over again. */
  698. res = client->GetBufferSize(&frames);
  699. if (FAILED(res))
  700. throw HRError("Failed to get buffer size", res);
  701. ComPtr<IAudioRenderClient> render;
  702. res = client->GetService(IID_PPV_ARGS(render.Assign()));
  703. if (FAILED(res))
  704. throw HRError("Failed to get render client", res);
  705. res = render->GetBuffer(frames, &buffer);
  706. if (FAILED(res))
  707. throw HRError("Failed to get buffer", res);
  708. memset(buffer, 0, (size_t)frames * (size_t)wfex->nBlockAlign);
  709. render->ReleaseBuffer(frames, 0);
  710. }
  711. static speaker_layout ConvertSpeakerLayout(DWORD layout, WORD channels)
  712. {
  713. switch (layout) {
  714. case KSAUDIO_SPEAKER_2POINT1:
  715. return SPEAKERS_2POINT1;
  716. case KSAUDIO_SPEAKER_SURROUND:
  717. return SPEAKERS_4POINT0;
  718. case OBS_KSAUDIO_SPEAKER_4POINT1:
  719. return SPEAKERS_4POINT1;
  720. case KSAUDIO_SPEAKER_5POINT1_SURROUND:
  721. return SPEAKERS_5POINT1;
  722. case KSAUDIO_SPEAKER_7POINT1_SURROUND:
  723. return SPEAKERS_7POINT1;
  724. }
  725. return (speaker_layout)channels;
  726. }
  727. void WASAPISource::InitFormat(const WAVEFORMATEX *wfex,
  728. enum speaker_layout &speakers,
  729. enum audio_format &format, uint32_t &sampleRate)
  730. {
  731. DWORD layout = 0;
  732. if (wfex->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
  733. WAVEFORMATEXTENSIBLE *ext = (WAVEFORMATEXTENSIBLE *)wfex;
  734. layout = ext->dwChannelMask;
  735. }
  736. /* WASAPI is always float */
  737. speakers = ConvertSpeakerLayout(layout, wfex->nChannels);
  738. format = AUDIO_FORMAT_FLOAT;
  739. sampleRate = wfex->nSamplesPerSec;
  740. }
  741. ComPtr<IAudioCaptureClient> WASAPISource::InitCapture(IAudioClient *client,
  742. HANDLE receiveSignal)
  743. {
  744. ComPtr<IAudioCaptureClient> capture;
  745. HRESULT res = client->GetService(IID_PPV_ARGS(capture.Assign()));
  746. if (FAILED(res))
  747. throw HRError("Failed to create capture context", res);
  748. res = client->SetEventHandle(receiveSignal);
  749. if (FAILED(res))
  750. throw HRError("Failed to set event handle", res);
  751. res = client->Start();
  752. if (FAILED(res))
  753. throw HRError("Failed to start capture client", res);
  754. return capture;
  755. }
  756. void WASAPISource::Initialize()
  757. {
  758. ComPtr<IMMDevice> device;
  759. if (sourceType == SourceType::ProcessOutput) {
  760. device_name = "[VIRTUAL_AUDIO_DEVICE_PROCESS_LOOPBACK]";
  761. hwnd = ms_find_window(INCLUDE_MINIMIZED, priority,
  762. window_class.c_str(), title.c_str(),
  763. executable.c_str());
  764. if (!hwnd)
  765. throw "Failed to find window";
  766. DWORD dwProcessId = 0;
  767. if (!GetWindowThreadProcessId(hwnd, &dwProcessId)) {
  768. hwnd = NULL;
  769. throw "Failed to get process id of window";
  770. }
  771. process_id = dwProcessId;
  772. } else {
  773. device = InitDevice(enumerator, isDefaultDevice, sourceType,
  774. device_id);
  775. device_name = GetDeviceName(device);
  776. }
  777. ResetEvent(receiveSignal);
  778. ComPtr<IAudioClient> temp_client = InitClient(
  779. device, sourceType, process_id, activate_audio_interface_async,
  780. speakers, format, sampleRate);
  781. if (sourceType == SourceType::DeviceOutput)
  782. ClearBuffer(device);
  783. ComPtr<IAudioCaptureClient> temp_capture =
  784. InitCapture(temp_client, receiveSignal);
  785. client = std::move(temp_client);
  786. capture = std::move(temp_capture);
  787. if (rtwq_supported) {
  788. HRESULT hr = rtwq_put_waiting_work_item(
  789. receiveSignal, 0, sampleReadyAsyncResult, nullptr);
  790. if (FAILED(hr)) {
  791. capture.Clear();
  792. client.Clear();
  793. throw HRError("RtwqPutWaitingWorkItem failed", hr);
  794. }
  795. hr = rtwq_put_waiting_work_item(restartSignal, 0,
  796. restartAsyncResult, nullptr);
  797. if (FAILED(hr)) {
  798. capture.Clear();
  799. client.Clear();
  800. throw HRError("RtwqPutWaitingWorkItem failed", hr);
  801. }
  802. }
  803. blog(LOG_INFO,
  804. "WASAPI: Device '%s' [%" PRIu32 " Hz] initialized (source: %s)",
  805. device_name.c_str(), sampleRate, obs_source_get_name(source));
  806. if (sourceType == SourceType::ProcessOutput && !hooked) {
  807. hooked = true;
  808. signal_handler_t *sh = obs_source_get_signal_handler(source);
  809. calldata_t data = {0};
  810. struct dstr title = {0};
  811. struct dstr window_class = {0};
  812. struct dstr executable = {0};
  813. ms_get_window_title(&title, hwnd);
  814. ms_get_window_class(&window_class, hwnd);
  815. ms_get_window_exe(&executable, hwnd);
  816. calldata_set_ptr(&data, "source", source);
  817. calldata_set_string(&data, "title", title.array);
  818. calldata_set_string(&data, "class", window_class.array);
  819. calldata_set_string(&data, "executable", executable.array);
  820. signal_handler_signal(sh, "hooked", &data);
  821. dstr_free(&title);
  822. dstr_free(&window_class);
  823. dstr_free(&executable);
  824. calldata_free(&data);
  825. }
  826. }
  827. bool WASAPISource::TryInitialize()
  828. {
  829. bool success = false;
  830. try {
  831. Initialize();
  832. success = true;
  833. } catch (HRError &error) {
  834. if (!previouslyFailed) {
  835. blog(LOG_WARNING,
  836. "[WASAPISource::TryInitialize]:[%s] %s: %lX",
  837. device_name.empty() ? device_id.c_str()
  838. : device_name.c_str(),
  839. error.str, error.hr);
  840. }
  841. } catch (const char *error) {
  842. if (!previouslyFailed) {
  843. blog(LOG_WARNING,
  844. "[WASAPISource::TryInitialize]:[%s] %s",
  845. device_name.empty() ? device_id.c_str()
  846. : device_name.c_str(),
  847. error);
  848. }
  849. }
  850. previouslyFailed = !success;
  851. return success;
  852. }
  853. DWORD WINAPI WASAPISource::ReconnectThread(LPVOID param)
  854. {
  855. os_set_thread_name("win-wasapi: reconnect thread");
  856. WASAPISource *source = (WASAPISource *)param;
  857. const HANDLE sigs[] = {
  858. source->reconnectExitSignal,
  859. source->reconnectSignal,
  860. };
  861. const HANDLE reconnect_sigs[] = {
  862. source->reconnectExitSignal,
  863. source->stopSignal,
  864. };
  865. bool exit = false;
  866. while (!exit) {
  867. const DWORD ret = WaitForMultipleObjects(_countof(sigs), sigs,
  868. false, INFINITE);
  869. switch (ret) {
  870. case WAIT_OBJECT_0:
  871. exit = true;
  872. break;
  873. default:
  874. assert(ret == (WAIT_OBJECT_0 + 1));
  875. if (source->reconnectDuration > 0) {
  876. WaitForMultipleObjects(
  877. _countof(reconnect_sigs),
  878. reconnect_sigs, false,
  879. source->reconnectDuration);
  880. }
  881. source->Start();
  882. }
  883. }
  884. return 0;
  885. }
  886. bool WASAPISource::ProcessCaptureData()
  887. {
  888. HRESULT res;
  889. LPBYTE buffer;
  890. UINT32 frames;
  891. DWORD flags;
  892. UINT64 pos, ts;
  893. UINT captureSize = 0;
  894. while (true) {
  895. if ((sourceType == SourceType::ProcessOutput) &&
  896. !IsWindow(hwnd)) {
  897. blog(LOG_WARNING,
  898. "[WASAPISource::ProcessCaptureData] window disappeared");
  899. return false;
  900. }
  901. res = capture->GetNextPacketSize(&captureSize);
  902. if (FAILED(res)) {
  903. if (res != AUDCLNT_E_DEVICE_INVALIDATED)
  904. blog(LOG_WARNING,
  905. "[WASAPISource::ProcessCaptureData]"
  906. " capture->GetNextPacketSize"
  907. " failed: %lX",
  908. res);
  909. return false;
  910. }
  911. if (!captureSize)
  912. break;
  913. res = capture->GetBuffer(&buffer, &frames, &flags, &pos, &ts);
  914. if (FAILED(res)) {
  915. if (res != AUDCLNT_E_DEVICE_INVALIDATED)
  916. blog(LOG_WARNING,
  917. "[WASAPISource::ProcessCaptureData]"
  918. " capture->GetBuffer"
  919. " failed: %lX",
  920. res);
  921. return false;
  922. }
  923. if (flags & AUDCLNT_BUFFERFLAGS_TIMESTAMP_ERROR) {
  924. blog(LOG_ERROR, "[WASAPISource::ProcessCaptureData]"
  925. " Timestamp error!");
  926. capture->ReleaseBuffer(frames);
  927. return false;
  928. }
  929. if (flags & AUDCLNT_BUFFERFLAGS_DATA_DISCONTINUITY) {
  930. /* libobs should handle discontinuities fine. */
  931. blog(LOG_DEBUG, "[WASAPISource::ProcessCaptureData]"
  932. " Discontinuity flag is set.");
  933. }
  934. if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
  935. blog(LOG_DEBUG, "[WASAPISource::ProcessCaptureData]"
  936. " Silent flag is set.");
  937. /* buffer size = frame size * number of frames
  938. * frame size = channels * sample size
  939. * sample size = 4 bytes (always float per InitFormat) */
  940. uint32_t requiredBufSize =
  941. get_audio_channels(speakers) * frames * 4;
  942. if (silence.size() < requiredBufSize)
  943. silence.resize(requiredBufSize);
  944. buffer = silence.data();
  945. }
  946. obs_source_audio data = {};
  947. data.data[0] = buffer;
  948. data.frames = frames;
  949. data.speakers = speakers;
  950. data.samples_per_sec = sampleRate;
  951. data.format = format;
  952. if (sourceType == SourceType::ProcessOutput) {
  953. data.timestamp = ts * 100;
  954. } else {
  955. data.timestamp = useDeviceTiming ? ts * 100
  956. : os_gettime_ns();
  957. if (!useDeviceTiming)
  958. data.timestamp -= util_mul_div64(
  959. frames, UINT64_C(1000000000),
  960. sampleRate);
  961. }
  962. obs_source_output_audio(source, &data);
  963. capture->ReleaseBuffer(frames);
  964. }
  965. return true;
  966. }
  967. #define RECONNECT_INTERVAL 3000
  968. DWORD WINAPI WASAPISource::CaptureThread(LPVOID param)
  969. {
  970. os_set_thread_name("win-wasapi: capture thread");
  971. const HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
  972. const bool com_initialized = SUCCEEDED(hr);
  973. if (!com_initialized) {
  974. blog(LOG_ERROR,
  975. "[WASAPISource::CaptureThread]"
  976. " CoInitializeEx failed: 0x%08X",
  977. hr);
  978. }
  979. DWORD unused = 0;
  980. const HANDLE handle = AvSetMmThreadCharacteristics(L"Audio", &unused);
  981. WASAPISource *source = (WASAPISource *)param;
  982. const HANDLE inactive_sigs[] = {
  983. source->exitSignal,
  984. source->stopSignal,
  985. source->initSignal,
  986. };
  987. const HANDLE active_sigs[] = {
  988. source->exitSignal,
  989. source->stopSignal,
  990. source->receiveSignal,
  991. source->restartSignal,
  992. };
  993. DWORD sig_count = _countof(inactive_sigs);
  994. const HANDLE *sigs = inactive_sigs;
  995. bool exit = false;
  996. while (!exit) {
  997. bool idle = false;
  998. bool stop = false;
  999. bool reconnect = false;
  1000. do {
  1001. /* Windows 7 does not seem to wake up for LOOPBACK */
  1002. const DWORD dwMilliseconds =
  1003. ((sigs == active_sigs) &&
  1004. (source->sourceType != SourceType::Input))
  1005. ? 10
  1006. : INFINITE;
  1007. const DWORD ret = WaitForMultipleObjects(
  1008. sig_count, sigs, false, dwMilliseconds);
  1009. switch (ret) {
  1010. case WAIT_OBJECT_0: {
  1011. exit = true;
  1012. stop = true;
  1013. idle = true;
  1014. break;
  1015. }
  1016. case WAIT_OBJECT_0 + 1:
  1017. stop = true;
  1018. idle = true;
  1019. break;
  1020. case WAIT_OBJECT_0 + 2:
  1021. case WAIT_TIMEOUT:
  1022. if (sigs == inactive_sigs) {
  1023. assert(ret != WAIT_TIMEOUT);
  1024. if (source->TryInitialize()) {
  1025. sig_count =
  1026. _countof(active_sigs);
  1027. sigs = active_sigs;
  1028. } else {
  1029. if (source->reconnectDuration ==
  1030. 0) {
  1031. blog(LOG_INFO,
  1032. "WASAPI: Device '%s' failed to start (source: %s)",
  1033. source->device_id
  1034. .c_str(),
  1035. obs_source_get_name(
  1036. source->source));
  1037. }
  1038. stop = true;
  1039. reconnect = true;
  1040. source->reconnectDuration =
  1041. RECONNECT_INTERVAL;
  1042. }
  1043. } else {
  1044. stop = !source->ProcessCaptureData();
  1045. if (stop) {
  1046. blog(LOG_INFO,
  1047. "Device '%s' invalidated. Retrying (source: %s)",
  1048. source->device_name.c_str(),
  1049. obs_source_get_name(
  1050. source->source));
  1051. if (source->sourceType ==
  1052. SourceType::
  1053. ProcessOutput &&
  1054. source->hooked) {
  1055. source->hooked = false;
  1056. signal_handler_t *sh =
  1057. obs_source_get_signal_handler(
  1058. source->source);
  1059. calldata_t data = {0};
  1060. calldata_set_ptr(
  1061. &data, "source",
  1062. source->source);
  1063. signal_handler_signal(
  1064. sh, "unhooked",
  1065. &data);
  1066. calldata_free(&data);
  1067. }
  1068. stop = true;
  1069. reconnect = true;
  1070. source->reconnectDuration =
  1071. RECONNECT_INTERVAL;
  1072. }
  1073. }
  1074. break;
  1075. default:
  1076. assert(sigs == active_sigs);
  1077. assert(ret == WAIT_OBJECT_0 + 3);
  1078. stop = true;
  1079. reconnect = true;
  1080. source->reconnectDuration = 0;
  1081. ResetEvent(source->restartSignal);
  1082. }
  1083. } while (!stop);
  1084. sig_count = _countof(inactive_sigs);
  1085. sigs = inactive_sigs;
  1086. if (source->client) {
  1087. source->client->Stop();
  1088. source->capture.Clear();
  1089. source->client.Clear();
  1090. }
  1091. if (idle) {
  1092. SetEvent(source->idleSignal);
  1093. } else if (reconnect) {
  1094. blog(LOG_INFO,
  1095. "Device '%s' invalidated. Retrying (source: %s)",
  1096. source->device_name.c_str(),
  1097. obs_source_get_name(source->source));
  1098. if (source->sourceType == SourceType::ProcessOutput &&
  1099. source->hooked) {
  1100. source->hooked = false;
  1101. signal_handler_t *sh =
  1102. obs_source_get_signal_handler(
  1103. source->source);
  1104. calldata_t data = {0};
  1105. calldata_set_ptr(&data, "source",
  1106. source->source);
  1107. signal_handler_signal(sh, "unhooked", &data);
  1108. calldata_free(&data);
  1109. }
  1110. SetEvent(source->reconnectSignal);
  1111. }
  1112. }
  1113. if (handle)
  1114. AvRevertMmThreadCharacteristics(handle);
  1115. if (com_initialized)
  1116. CoUninitialize();
  1117. return 0;
  1118. }
  1119. void WASAPISource::SetDefaultDevice(EDataFlow flow, ERole role, LPCWSTR id)
  1120. {
  1121. if (!isDefaultDevice)
  1122. return;
  1123. const bool input = sourceType == SourceType::Input;
  1124. const EDataFlow expectedFlow = input ? eCapture : eRender;
  1125. const ERole expectedRole = input ? eCommunications : eConsole;
  1126. if (flow != expectedFlow || role != expectedRole)
  1127. return;
  1128. if (id) {
  1129. if (default_id.compare(id) == 0)
  1130. return;
  1131. default_id = id;
  1132. } else {
  1133. if (default_id.empty())
  1134. return;
  1135. default_id.clear();
  1136. }
  1137. blog(LOG_INFO, "WASAPI: Default %s device changed",
  1138. input ? "input" : "output");
  1139. SetEvent(restartSignal);
  1140. }
  1141. void WASAPISource::OnStartCapture()
  1142. {
  1143. const DWORD ret = WaitForSingleObject(stopSignal, 0);
  1144. switch (ret) {
  1145. case WAIT_OBJECT_0:
  1146. SetEvent(idleSignal);
  1147. break;
  1148. default:
  1149. assert(ret == WAIT_TIMEOUT);
  1150. if (!TryInitialize()) {
  1151. if (reconnectDuration == 0) {
  1152. blog(LOG_INFO,
  1153. "WASAPI: Device '%s' failed to start (source: %s)",
  1154. device_id.c_str(),
  1155. obs_source_get_name(source));
  1156. }
  1157. reconnectDuration = RECONNECT_INTERVAL;
  1158. SetEvent(reconnectSignal);
  1159. }
  1160. }
  1161. }
  1162. void WASAPISource::OnSampleReady()
  1163. {
  1164. bool stop = false;
  1165. bool reconnect = false;
  1166. if (!ProcessCaptureData()) {
  1167. stop = true;
  1168. reconnect = true;
  1169. reconnectDuration = RECONNECT_INTERVAL;
  1170. }
  1171. if (WaitForSingleObject(restartSignal, 0) == WAIT_OBJECT_0) {
  1172. stop = true;
  1173. reconnect = true;
  1174. reconnectDuration = 0;
  1175. ResetEvent(restartSignal);
  1176. rtwq_put_waiting_work_item(restartSignal, 0, restartAsyncResult,
  1177. nullptr);
  1178. }
  1179. if (WaitForSingleObject(stopSignal, 0) == WAIT_OBJECT_0) {
  1180. stop = true;
  1181. reconnect = false;
  1182. }
  1183. if (!stop) {
  1184. if (FAILED(rtwq_put_waiting_work_item(receiveSignal, 0,
  1185. sampleReadyAsyncResult,
  1186. nullptr))) {
  1187. blog(LOG_ERROR,
  1188. "Could not requeue sample receive work");
  1189. stop = true;
  1190. reconnect = true;
  1191. reconnectDuration = RECONNECT_INTERVAL;
  1192. }
  1193. }
  1194. if (stop) {
  1195. client->Stop();
  1196. capture.Clear();
  1197. client.Clear();
  1198. if (reconnect) {
  1199. blog(LOG_INFO,
  1200. "Device '%s' invalidated. Retrying (source: %s)",
  1201. device_name.c_str(), obs_source_get_name(source));
  1202. SetEvent(reconnectSignal);
  1203. if (sourceType == SourceType::ProcessOutput && hooked) {
  1204. hooked = false;
  1205. signal_handler_t *sh =
  1206. obs_source_get_signal_handler(source);
  1207. calldata_t data = {0};
  1208. calldata_set_ptr(&data, "source", source);
  1209. signal_handler_signal(sh, "unhooked", &data);
  1210. calldata_free(&data);
  1211. }
  1212. } else {
  1213. SetEvent(idleSignal);
  1214. }
  1215. }
  1216. }
  1217. void WASAPISource::OnRestart()
  1218. {
  1219. SetEvent(receiveSignal);
  1220. }
  1221. bool WASAPISource::GetHooked()
  1222. {
  1223. return hooked;
  1224. }
  1225. HWND WASAPISource::GetHwnd()
  1226. {
  1227. return hwnd;
  1228. }
  1229. /* ------------------------------------------------------------------------- */
  1230. static const char *GetWASAPIInputName(void *)
  1231. {
  1232. return obs_module_text("AudioInput");
  1233. }
  1234. static const char *GetWASAPIDeviceOutputName(void *)
  1235. {
  1236. return obs_module_text("AudioOutput");
  1237. }
  1238. static const char *GetWASAPIProcessOutputName(void *)
  1239. {
  1240. return obs_module_text("ApplicationAudioCapture");
  1241. }
  1242. static void GetWASAPIDefaultsInput(obs_data_t *settings)
  1243. {
  1244. obs_data_set_default_string(settings, OPT_DEVICE_ID, "default");
  1245. obs_data_set_default_bool(settings, OPT_USE_DEVICE_TIMING, false);
  1246. }
  1247. static void GetWASAPIDefaultsDeviceOutput(obs_data_t *settings)
  1248. {
  1249. obs_data_set_default_string(settings, OPT_DEVICE_ID, "default");
  1250. obs_data_set_default_bool(settings, OPT_USE_DEVICE_TIMING, true);
  1251. }
  1252. static void GetWASAPIDefaultsProcessOutput(obs_data_t *) {}
  1253. static void wasapi_get_hooked(void *data, calldata_t *cd)
  1254. {
  1255. WASAPISource *wasapi_source = reinterpret_cast<WASAPISource *>(data);
  1256. if (!wasapi_source)
  1257. return;
  1258. bool hooked = wasapi_source->GetHooked();
  1259. HWND hwnd = wasapi_source->GetHwnd();
  1260. if (hooked && hwnd) {
  1261. calldata_set_bool(cd, "hooked", true);
  1262. struct dstr title = {0};
  1263. struct dstr window_class = {0};
  1264. struct dstr executable = {0};
  1265. ms_get_window_title(&title, hwnd);
  1266. ms_get_window_class(&window_class, hwnd);
  1267. ms_get_window_exe(&executable, hwnd);
  1268. calldata_set_string(cd, "title", title.array);
  1269. calldata_set_string(cd, "class", window_class.array);
  1270. calldata_set_string(cd, "executable", executable.array);
  1271. dstr_free(&title);
  1272. dstr_free(&window_class);
  1273. dstr_free(&executable);
  1274. } else {
  1275. calldata_set_bool(cd, "hooked", false);
  1276. calldata_set_string(cd, "title", "");
  1277. calldata_set_string(cd, "class", "");
  1278. calldata_set_string(cd, "executable", "");
  1279. }
  1280. }
  1281. static void *CreateWASAPISource(obs_data_t *settings, obs_source_t *source,
  1282. SourceType type)
  1283. {
  1284. try {
  1285. if (type != SourceType::ProcessOutput) {
  1286. return new WASAPISource(settings, source, type);
  1287. } else {
  1288. WASAPISource *wasapi_source =
  1289. new WASAPISource(settings, source, type);
  1290. if (wasapi_source) {
  1291. signal_handler_t *sh =
  1292. obs_source_get_signal_handler(source);
  1293. signal_handler_add(sh,
  1294. "void unhooked(ptr source)");
  1295. signal_handler_add(
  1296. sh,
  1297. "void hooked(ptr source, string title, string class, string executable)");
  1298. proc_handler_t *ph =
  1299. obs_source_get_proc_handler(source);
  1300. proc_handler_add(
  1301. ph,
  1302. "void get_hooked(out bool hooked, out string title, out string class, out string executable)",
  1303. wasapi_get_hooked, wasapi_source);
  1304. }
  1305. return wasapi_source;
  1306. }
  1307. } catch (const char *error) {
  1308. blog(LOG_ERROR, "[CreateWASAPISource] %s", error);
  1309. }
  1310. return nullptr;
  1311. }
  1312. static void *CreateWASAPIInput(obs_data_t *settings, obs_source_t *source)
  1313. {
  1314. return CreateWASAPISource(settings, source, SourceType::Input);
  1315. }
  1316. static void *CreateWASAPIDeviceOutput(obs_data_t *settings,
  1317. obs_source_t *source)
  1318. {
  1319. return CreateWASAPISource(settings, source, SourceType::DeviceOutput);
  1320. }
  1321. static void *CreateWASAPIProcessOutput(obs_data_t *settings,
  1322. obs_source_t *source)
  1323. {
  1324. return CreateWASAPISource(settings, source, SourceType::ProcessOutput);
  1325. }
  1326. static void DestroyWASAPISource(void *obj)
  1327. {
  1328. delete static_cast<WASAPISource *>(obj);
  1329. }
  1330. static void UpdateWASAPISource(void *obj, obs_data_t *settings)
  1331. {
  1332. static_cast<WASAPISource *>(obj)->Update(settings);
  1333. }
  1334. static void ActivateWASAPISource(void *obj)
  1335. {
  1336. static_cast<WASAPISource *>(obj)->Activate();
  1337. }
  1338. static void DeactivateWASAPISource(void *obj)
  1339. {
  1340. static_cast<WASAPISource *>(obj)->Deactivate();
  1341. }
  1342. static bool UpdateWASAPIMethod(obs_properties_t *props, obs_property_t *,
  1343. obs_data_t *settings)
  1344. {
  1345. WASAPISource *source = (WASAPISource *)obs_properties_get_param(props);
  1346. if (!source)
  1347. return false;
  1348. source->Update(settings);
  1349. return true;
  1350. }
  1351. static obs_properties_t *GetWASAPIPropertiesInput(void *)
  1352. {
  1353. obs_properties_t *props = obs_properties_create();
  1354. vector<AudioDeviceInfo> devices;
  1355. obs_property_t *device_prop = obs_properties_add_list(
  1356. props, OPT_DEVICE_ID, obs_module_text("Device"),
  1357. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  1358. GetWASAPIAudioDevices(devices, true);
  1359. if (devices.size())
  1360. obs_property_list_add_string(
  1361. device_prop, obs_module_text("Default"), "default");
  1362. for (size_t i = 0; i < devices.size(); i++) {
  1363. AudioDeviceInfo &device = devices[i];
  1364. obs_property_list_add_string(device_prop, device.name.c_str(),
  1365. device.id.c_str());
  1366. }
  1367. obs_properties_add_bool(props, OPT_USE_DEVICE_TIMING,
  1368. obs_module_text("UseDeviceTiming"));
  1369. return props;
  1370. }
  1371. static obs_properties_t *GetWASAPIPropertiesDeviceOutput(void *)
  1372. {
  1373. obs_properties_t *props = obs_properties_create();
  1374. vector<AudioDeviceInfo> devices;
  1375. obs_property_t *device_prop = obs_properties_add_list(
  1376. props, OPT_DEVICE_ID, obs_module_text("Device"),
  1377. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  1378. GetWASAPIAudioDevices(devices, false);
  1379. if (devices.size())
  1380. obs_property_list_add_string(
  1381. device_prop, obs_module_text("Default"), "default");
  1382. for (size_t i = 0; i < devices.size(); i++) {
  1383. AudioDeviceInfo &device = devices[i];
  1384. obs_property_list_add_string(device_prop, device.name.c_str(),
  1385. device.id.c_str());
  1386. }
  1387. obs_properties_add_bool(props, OPT_USE_DEVICE_TIMING,
  1388. obs_module_text("UseDeviceTiming"));
  1389. return props;
  1390. }
  1391. static bool wasapi_window_changed(obs_properties_t *props, obs_property_t *p,
  1392. obs_data_t *settings)
  1393. {
  1394. WASAPISource *source = (WASAPISource *)obs_properties_get_param(props);
  1395. if (!source)
  1396. return false;
  1397. source->OnWindowChanged(settings);
  1398. ms_check_window_property_setting(props, p, settings, "window", 0);
  1399. return true;
  1400. }
  1401. static obs_properties_t *GetWASAPIPropertiesProcessOutput(void *data)
  1402. {
  1403. obs_properties_t *props = obs_properties_create();
  1404. obs_properties_set_param(props, data, NULL);
  1405. obs_property_t *const window_prop = obs_properties_add_list(
  1406. props, OPT_WINDOW, obs_module_text("Window"),
  1407. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  1408. ms_fill_window_list(window_prop, INCLUDE_MINIMIZED, nullptr);
  1409. obs_property_set_modified_callback(window_prop, wasapi_window_changed);
  1410. obs_property_t *const priority_prop = obs_properties_add_list(
  1411. props, OPT_PRIORITY, obs_module_text("Priority"),
  1412. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  1413. obs_property_list_add_int(priority_prop,
  1414. obs_module_text("Priority.Title"),
  1415. WINDOW_PRIORITY_TITLE);
  1416. obs_property_list_add_int(priority_prop,
  1417. obs_module_text("Priority.Class"),
  1418. WINDOW_PRIORITY_CLASS);
  1419. obs_property_list_add_int(priority_prop,
  1420. obs_module_text("Priority.Exe"),
  1421. WINDOW_PRIORITY_EXE);
  1422. return props;
  1423. }
  1424. void RegisterWASAPIInput()
  1425. {
  1426. obs_source_info info = {};
  1427. info.id = "wasapi_input_capture";
  1428. info.type = OBS_SOURCE_TYPE_INPUT;
  1429. info.output_flags = OBS_SOURCE_AUDIO | OBS_SOURCE_DO_NOT_DUPLICATE;
  1430. info.get_name = GetWASAPIInputName;
  1431. info.create = CreateWASAPIInput;
  1432. info.destroy = DestroyWASAPISource;
  1433. info.update = UpdateWASAPISource;
  1434. info.activate = ActivateWASAPISource;
  1435. info.deactivate = DeactivateWASAPISource;
  1436. info.get_defaults = GetWASAPIDefaultsInput;
  1437. info.get_properties = GetWASAPIPropertiesInput;
  1438. info.icon_type = OBS_ICON_TYPE_AUDIO_INPUT;
  1439. obs_register_source(&info);
  1440. }
  1441. void RegisterWASAPIDeviceOutput()
  1442. {
  1443. obs_source_info info = {};
  1444. info.id = "wasapi_output_capture";
  1445. info.type = OBS_SOURCE_TYPE_INPUT;
  1446. info.output_flags = OBS_SOURCE_AUDIO | OBS_SOURCE_DO_NOT_DUPLICATE |
  1447. OBS_SOURCE_DO_NOT_SELF_MONITOR;
  1448. info.get_name = GetWASAPIDeviceOutputName;
  1449. info.create = CreateWASAPIDeviceOutput;
  1450. info.destroy = DestroyWASAPISource;
  1451. info.update = UpdateWASAPISource;
  1452. info.activate = ActivateWASAPISource;
  1453. info.deactivate = DeactivateWASAPISource;
  1454. info.get_defaults = GetWASAPIDefaultsDeviceOutput;
  1455. info.get_properties = GetWASAPIPropertiesDeviceOutput;
  1456. info.icon_type = OBS_ICON_TYPE_AUDIO_OUTPUT;
  1457. obs_register_source(&info);
  1458. }
  1459. void RegisterWASAPIProcessOutput()
  1460. {
  1461. obs_source_info info = {};
  1462. info.id = "wasapi_process_output_capture";
  1463. info.type = OBS_SOURCE_TYPE_INPUT;
  1464. info.output_flags = OBS_SOURCE_AUDIO | OBS_SOURCE_DO_NOT_DUPLICATE |
  1465. OBS_SOURCE_DO_NOT_SELF_MONITOR;
  1466. info.get_name = GetWASAPIProcessOutputName;
  1467. info.create = CreateWASAPIProcessOutput;
  1468. info.destroy = DestroyWASAPISource;
  1469. info.update = UpdateWASAPISource;
  1470. info.activate = ActivateWASAPISource;
  1471. info.deactivate = DeactivateWASAPISource;
  1472. info.get_defaults = GetWASAPIDefaultsProcessOutput;
  1473. info.get_properties = GetWASAPIPropertiesProcessOutput;
  1474. info.icon_type = OBS_ICON_TYPE_PROCESS_AUDIO_OUTPUT;
  1475. obs_register_source(&info);
  1476. }