win-wasapi.cpp 44 KB

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