win-wasapi.cpp 45 KB

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