win-wasapi.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. #include "enum-wasapi.hpp"
  2. #include <obs-module.h>
  3. #include <obs.h>
  4. #include <util/platform.h>
  5. #include <util/windows/HRError.hpp>
  6. #include <util/windows/ComPtr.hpp>
  7. #include <util/windows/WinHandle.hpp>
  8. #include <util/windows/CoTaskMemPtr.hpp>
  9. #include <util/threading.h>
  10. #include <util/util_uint64.h>
  11. #include <atomic>
  12. #include <cinttypes>
  13. #include <avrt.h>
  14. #include <RTWorkQ.h>
  15. using namespace std;
  16. #define OPT_DEVICE_ID "device_id"
  17. #define OPT_USE_DEVICE_TIMING "use_device_timing"
  18. static void GetWASAPIDefaults(obs_data_t *settings);
  19. #define OBS_KSAUDIO_SPEAKER_4POINT1 \
  20. (KSAUDIO_SPEAKER_SURROUND | SPEAKER_LOW_FREQUENCY)
  21. typedef HRESULT(STDAPICALLTYPE *PFN_RtwqUnlockWorkQueue)(DWORD);
  22. typedef HRESULT(STDAPICALLTYPE *PFN_RtwqLockSharedWorkQueue)(PCWSTR usageClass,
  23. LONG basePriority,
  24. DWORD *taskId,
  25. DWORD *id);
  26. typedef HRESULT(STDAPICALLTYPE *PFN_RtwqCreateAsyncResult)(IUnknown *,
  27. IRtwqAsyncCallback *,
  28. IUnknown *,
  29. IRtwqAsyncResult **);
  30. typedef HRESULT(STDAPICALLTYPE *PFN_RtwqPutWorkItem)(DWORD, LONG,
  31. IRtwqAsyncResult *);
  32. typedef HRESULT(STDAPICALLTYPE *PFN_RtwqPutWaitingWorkItem)(HANDLE, LONG,
  33. IRtwqAsyncResult *,
  34. RTWQWORKITEM_KEY *);
  35. class ARtwqAsyncCallback : public IRtwqAsyncCallback {
  36. protected:
  37. ARtwqAsyncCallback(void *source) : source(source) {}
  38. public:
  39. STDMETHOD_(ULONG, AddRef)() { return ++refCount; }
  40. STDMETHOD_(ULONG, Release)() { return --refCount; }
  41. STDMETHOD(QueryInterface)(REFIID riid, void **ppvObject)
  42. {
  43. HRESULT hr = E_NOINTERFACE;
  44. if (riid == __uuidof(IRtwqAsyncCallback) ||
  45. riid == __uuidof(IUnknown)) {
  46. *ppvObject = this;
  47. AddRef();
  48. hr = S_OK;
  49. } else {
  50. *ppvObject = NULL;
  51. }
  52. return hr;
  53. }
  54. STDMETHOD(GetParameters)
  55. (DWORD *pdwFlags, DWORD *pdwQueue)
  56. {
  57. *pdwFlags = 0;
  58. *pdwQueue = queue_id;
  59. return S_OK;
  60. }
  61. STDMETHOD(Invoke)
  62. (IRtwqAsyncResult *) override = 0;
  63. DWORD GetQueueId() const { return queue_id; }
  64. void SetQueueId(DWORD id) { queue_id = id; }
  65. protected:
  66. std::atomic<ULONG> refCount = 1;
  67. void *source;
  68. DWORD queue_id = 0;
  69. };
  70. class WASAPISource {
  71. ComPtr<IMMNotificationClient> notify;
  72. ComPtr<IMMDeviceEnumerator> enumerator;
  73. ComPtr<IAudioClient> client;
  74. ComPtr<IAudioCaptureClient> capture;
  75. obs_source_t *source;
  76. wstring default_id;
  77. string device_id;
  78. string device_name;
  79. PFN_RtwqUnlockWorkQueue rtwq_unlock_work_queue = NULL;
  80. PFN_RtwqLockSharedWorkQueue rtwq_lock_shared_work_queue = NULL;
  81. PFN_RtwqCreateAsyncResult rtwq_create_async_result = NULL;
  82. PFN_RtwqPutWorkItem rtwq_put_work_item = NULL;
  83. PFN_RtwqPutWaitingWorkItem rtwq_put_waiting_work_item = NULL;
  84. bool rtwq_supported = false;
  85. const bool isInputDevice;
  86. std::atomic<bool> useDeviceTiming = false;
  87. std::atomic<bool> isDefaultDevice = false;
  88. bool previouslyFailed = false;
  89. WinHandle reconnectThread;
  90. class CallbackStartCapture : public ARtwqAsyncCallback {
  91. public:
  92. CallbackStartCapture(WASAPISource *source)
  93. : ARtwqAsyncCallback(source)
  94. {
  95. }
  96. STDMETHOD(Invoke)
  97. (IRtwqAsyncResult *) override
  98. {
  99. ((WASAPISource *)source)->OnStartCapture();
  100. return S_OK;
  101. }
  102. } startCapture;
  103. ComPtr<IRtwqAsyncResult> startCaptureAsyncResult;
  104. class CallbackSampleReady : public ARtwqAsyncCallback {
  105. public:
  106. CallbackSampleReady(WASAPISource *source)
  107. : ARtwqAsyncCallback(source)
  108. {
  109. }
  110. STDMETHOD(Invoke)
  111. (IRtwqAsyncResult *) override
  112. {
  113. ((WASAPISource *)source)->OnSampleReady();
  114. return S_OK;
  115. }
  116. } sampleReady;
  117. ComPtr<IRtwqAsyncResult> sampleReadyAsyncResult;
  118. class CallbackRestart : public ARtwqAsyncCallback {
  119. public:
  120. CallbackRestart(WASAPISource *source)
  121. : ARtwqAsyncCallback(source)
  122. {
  123. }
  124. STDMETHOD(Invoke)
  125. (IRtwqAsyncResult *) override
  126. {
  127. ((WASAPISource *)source)->OnRestart();
  128. return S_OK;
  129. }
  130. } restart;
  131. ComPtr<IRtwqAsyncResult> restartAsyncResult;
  132. WinHandle captureThread;
  133. WinHandle idleSignal;
  134. WinHandle stopSignal;
  135. WinHandle receiveSignal;
  136. WinHandle restartSignal;
  137. WinHandle exitSignal;
  138. WinHandle initSignal;
  139. DWORD reconnectDuration = 0;
  140. WinHandle reconnectSignal;
  141. speaker_layout speakers;
  142. audio_format format;
  143. uint32_t sampleRate;
  144. static DWORD WINAPI ReconnectThread(LPVOID param);
  145. static DWORD WINAPI CaptureThread(LPVOID param);
  146. bool ProcessCaptureData();
  147. void Start();
  148. void Stop();
  149. static ComPtr<IMMDevice> InitDevice(IMMDeviceEnumerator *enumerator,
  150. bool isDefaultDevice,
  151. bool isInputDevice,
  152. const string device_id);
  153. static ComPtr<IAudioClient> InitClient(IMMDevice *device,
  154. bool isInputDevice,
  155. enum speaker_layout &speakers,
  156. enum audio_format &format,
  157. uint32_t &sampleRate);
  158. static void InitFormat(const WAVEFORMATEX *wfex,
  159. enum speaker_layout &speakers,
  160. enum audio_format &format, uint32_t &sampleRate);
  161. static void ClearBuffer(IMMDevice *device);
  162. static ComPtr<IAudioCaptureClient> InitCapture(IAudioClient *client,
  163. HANDLE receiveSignal);
  164. void Initialize();
  165. bool TryInitialize();
  166. void UpdateSettings(obs_data_t *settings);
  167. public:
  168. WASAPISource(obs_data_t *settings, obs_source_t *source_, bool input);
  169. ~WASAPISource();
  170. void Update(obs_data_t *settings);
  171. void SetDefaultDevice(EDataFlow flow, ERole role, LPCWSTR id);
  172. void OnStartCapture();
  173. void OnSampleReady();
  174. void OnRestart();
  175. };
  176. class WASAPINotify : public IMMNotificationClient {
  177. long refs = 0; /* auto-incremented to 1 by ComPtr */
  178. WASAPISource *source;
  179. public:
  180. WASAPINotify(WASAPISource *source_) : source(source_) {}
  181. STDMETHODIMP_(ULONG) AddRef()
  182. {
  183. return (ULONG)os_atomic_inc_long(&refs);
  184. }
  185. STDMETHODIMP_(ULONG) STDMETHODCALLTYPE Release()
  186. {
  187. long val = os_atomic_dec_long(&refs);
  188. if (val == 0)
  189. delete this;
  190. return (ULONG)val;
  191. }
  192. STDMETHODIMP QueryInterface(REFIID riid, void **ptr)
  193. {
  194. if (riid == IID_IUnknown) {
  195. *ptr = (IUnknown *)this;
  196. } else if (riid == __uuidof(IMMNotificationClient)) {
  197. *ptr = (IMMNotificationClient *)this;
  198. } else {
  199. *ptr = nullptr;
  200. return E_NOINTERFACE;
  201. }
  202. os_atomic_inc_long(&refs);
  203. return S_OK;
  204. }
  205. STDMETHODIMP OnDefaultDeviceChanged(EDataFlow flow, ERole role,
  206. LPCWSTR id)
  207. {
  208. source->SetDefaultDevice(flow, role, id);
  209. return S_OK;
  210. }
  211. STDMETHODIMP OnDeviceAdded(LPCWSTR) { return S_OK; }
  212. STDMETHODIMP OnDeviceRemoved(LPCWSTR) { return S_OK; }
  213. STDMETHODIMP OnDeviceStateChanged(LPCWSTR, DWORD) { return S_OK; }
  214. STDMETHODIMP OnPropertyValueChanged(LPCWSTR, const PROPERTYKEY)
  215. {
  216. return S_OK;
  217. }
  218. };
  219. WASAPISource::WASAPISource(obs_data_t *settings, obs_source_t *source_,
  220. bool input)
  221. : source(source_),
  222. isInputDevice(input),
  223. startCapture(this),
  224. sampleReady(this),
  225. restart(this)
  226. {
  227. UpdateSettings(settings);
  228. idleSignal = CreateEvent(nullptr, true, false, nullptr);
  229. if (!idleSignal.Valid())
  230. throw "Could not create idle signal";
  231. stopSignal = CreateEvent(nullptr, true, false, nullptr);
  232. if (!stopSignal.Valid())
  233. throw "Could not create stop signal";
  234. receiveSignal = CreateEvent(nullptr, false, false, nullptr);
  235. if (!receiveSignal.Valid())
  236. throw "Could not create receive signal";
  237. restartSignal = CreateEvent(nullptr, true, false, nullptr);
  238. if (!restartSignal.Valid())
  239. throw "Could not create restart signal";
  240. exitSignal = CreateEvent(nullptr, true, false, nullptr);
  241. if (!exitSignal.Valid())
  242. throw "Could not create exit signal";
  243. initSignal = CreateEvent(nullptr, false, false, nullptr);
  244. if (!initSignal.Valid())
  245. throw "Could not create init signal";
  246. reconnectSignal = CreateEvent(nullptr, false, false, nullptr);
  247. if (!reconnectSignal.Valid())
  248. throw "Could not create reconnect signal";
  249. reconnectThread = CreateThread(
  250. nullptr, 0, WASAPISource::ReconnectThread, this, 0, nullptr);
  251. if (!reconnectThread.Valid())
  252. throw "Failed to create reconnect thread";
  253. notify = new WASAPINotify(this);
  254. if (!notify)
  255. throw "Could not create WASAPINotify";
  256. HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr,
  257. CLSCTX_ALL,
  258. IID_PPV_ARGS(enumerator.Assign()));
  259. if (FAILED(hr))
  260. throw HRError("Failed to create enumerator", hr);
  261. hr = enumerator->RegisterEndpointNotificationCallback(notify);
  262. if (FAILED(hr))
  263. throw HRError("Failed to register endpoint callback", hr);
  264. /* OBS will already load DLL on startup if it exists */
  265. const HMODULE rtwq_module = GetModuleHandle(L"RTWorkQ.dll");
  266. rtwq_supported = rtwq_module != NULL;
  267. if (rtwq_supported) {
  268. rtwq_unlock_work_queue =
  269. (PFN_RtwqUnlockWorkQueue)GetProcAddress(
  270. rtwq_module, "RtwqUnlockWorkQueue");
  271. rtwq_lock_shared_work_queue =
  272. (PFN_RtwqLockSharedWorkQueue)GetProcAddress(
  273. rtwq_module, "RtwqLockSharedWorkQueue");
  274. rtwq_create_async_result =
  275. (PFN_RtwqCreateAsyncResult)GetProcAddress(
  276. rtwq_module, "RtwqCreateAsyncResult");
  277. rtwq_put_work_item = (PFN_RtwqPutWorkItem)GetProcAddress(
  278. rtwq_module, "RtwqPutWorkItem");
  279. rtwq_put_waiting_work_item =
  280. (PFN_RtwqPutWaitingWorkItem)GetProcAddress(
  281. rtwq_module, "RtwqPutWaitingWorkItem");
  282. hr = rtwq_create_async_result(nullptr, &startCapture, nullptr,
  283. &startCaptureAsyncResult);
  284. if (FAILED(hr)) {
  285. enumerator->UnregisterEndpointNotificationCallback(
  286. notify);
  287. throw HRError(
  288. "Could not create startCaptureAsyncResult", hr);
  289. }
  290. hr = rtwq_create_async_result(nullptr, &sampleReady, nullptr,
  291. &sampleReadyAsyncResult);
  292. if (FAILED(hr)) {
  293. enumerator->UnregisterEndpointNotificationCallback(
  294. notify);
  295. throw HRError("Could not create sampleReadyAsyncResult",
  296. hr);
  297. }
  298. hr = rtwq_create_async_result(nullptr, &restart, nullptr,
  299. &restartAsyncResult);
  300. if (FAILED(hr)) {
  301. enumerator->UnregisterEndpointNotificationCallback(
  302. notify);
  303. throw HRError("Could not create restartAsyncResult",
  304. hr);
  305. }
  306. DWORD taskId = 0;
  307. DWORD id = 0;
  308. hr = rtwq_lock_shared_work_queue(L"Capture", 0, &taskId, &id);
  309. if (FAILED(hr)) {
  310. enumerator->UnregisterEndpointNotificationCallback(
  311. notify);
  312. throw HRError("RtwqLockSharedWorkQueue failed", hr);
  313. }
  314. startCapture.SetQueueId(id);
  315. sampleReady.SetQueueId(id);
  316. restart.SetQueueId(id);
  317. } else {
  318. captureThread = CreateThread(nullptr, 0,
  319. WASAPISource::CaptureThread, this,
  320. 0, nullptr);
  321. if (!captureThread.Valid()) {
  322. enumerator->UnregisterEndpointNotificationCallback(
  323. notify);
  324. throw "Failed to create capture thread";
  325. }
  326. }
  327. Start();
  328. }
  329. void WASAPISource::Start()
  330. {
  331. if (rtwq_supported) {
  332. rtwq_put_work_item(startCapture.GetQueueId(), 0,
  333. startCaptureAsyncResult);
  334. } else {
  335. SetEvent(initSignal);
  336. }
  337. }
  338. void WASAPISource::Stop()
  339. {
  340. SetEvent(stopSignal);
  341. blog(LOG_INFO, "WASAPI: Device '%s' Terminated", device_name.c_str());
  342. if (rtwq_supported)
  343. SetEvent(receiveSignal);
  344. WaitForSingleObject(idleSignal, INFINITE);
  345. SetEvent(exitSignal);
  346. WaitForSingleObject(reconnectThread, INFINITE);
  347. if (rtwq_supported)
  348. rtwq_unlock_work_queue(sampleReady.GetQueueId());
  349. else
  350. WaitForSingleObject(captureThread, INFINITE);
  351. }
  352. WASAPISource::~WASAPISource()
  353. {
  354. enumerator->UnregisterEndpointNotificationCallback(notify);
  355. Stop();
  356. }
  357. void WASAPISource::UpdateSettings(obs_data_t *settings)
  358. {
  359. device_id = obs_data_get_string(settings, OPT_DEVICE_ID);
  360. useDeviceTiming = obs_data_get_bool(settings, OPT_USE_DEVICE_TIMING);
  361. isDefaultDevice = _strcmpi(device_id.c_str(), "default") == 0;
  362. blog(LOG_INFO,
  363. "[win-wasapi: '%s'] update settings:\n"
  364. "\tdevice id: %s\n"
  365. "\tuse device timing: %d",
  366. obs_source_get_name(source), device_id.c_str(),
  367. (int)useDeviceTiming);
  368. }
  369. void WASAPISource::Update(obs_data_t *settings)
  370. {
  371. const string newDevice = obs_data_get_string(settings, OPT_DEVICE_ID);
  372. const bool restart = newDevice.compare(device_id) != 0;
  373. UpdateSettings(settings);
  374. if (restart)
  375. SetEvent(restartSignal);
  376. }
  377. ComPtr<IMMDevice> WASAPISource::InitDevice(IMMDeviceEnumerator *enumerator,
  378. bool isDefaultDevice,
  379. bool isInputDevice,
  380. const string device_id)
  381. {
  382. ComPtr<IMMDevice> device;
  383. if (isDefaultDevice) {
  384. HRESULT res = enumerator->GetDefaultAudioEndpoint(
  385. isInputDevice ? eCapture : eRender,
  386. isInputDevice ? eCommunications : eConsole,
  387. device.Assign());
  388. if (FAILED(res))
  389. throw HRError("Failed GetDefaultAudioEndpoint", res);
  390. } else {
  391. wchar_t *w_id;
  392. os_utf8_to_wcs_ptr(device_id.c_str(), device_id.size(), &w_id);
  393. if (!w_id)
  394. throw "Failed to widen device id string";
  395. const HRESULT res =
  396. enumerator->GetDevice(w_id, device.Assign());
  397. bfree(w_id);
  398. if (FAILED(res))
  399. throw HRError("Failed to enumerate device", res);
  400. }
  401. return device;
  402. }
  403. #define BUFFER_TIME_100NS (5 * 10000000)
  404. ComPtr<IAudioClient> WASAPISource::InitClient(IMMDevice *device,
  405. bool isInputDevice,
  406. enum speaker_layout &speakers,
  407. enum audio_format &format,
  408. uint32_t &sampleRate)
  409. {
  410. ComPtr<IAudioClient> client;
  411. HRESULT res = device->Activate(__uuidof(IAudioClient), CLSCTX_ALL,
  412. nullptr, (void **)client.Assign());
  413. if (FAILED(res))
  414. throw HRError("Failed to activate client context", res);
  415. CoTaskMemPtr<WAVEFORMATEX> wfex;
  416. res = client->GetMixFormat(&wfex);
  417. if (FAILED(res))
  418. throw HRError("Failed to get mix format", res);
  419. InitFormat(wfex, speakers, format, sampleRate);
  420. DWORD flags = AUDCLNT_STREAMFLAGS_EVENTCALLBACK;
  421. if (!isInputDevice)
  422. flags |= AUDCLNT_STREAMFLAGS_LOOPBACK;
  423. res = client->Initialize(AUDCLNT_SHAREMODE_SHARED, flags,
  424. BUFFER_TIME_100NS, 0, wfex, nullptr);
  425. if (FAILED(res))
  426. throw HRError("Failed to initialize audio client", res);
  427. return client;
  428. }
  429. void WASAPISource::ClearBuffer(IMMDevice *device)
  430. {
  431. CoTaskMemPtr<WAVEFORMATEX> wfex;
  432. HRESULT res;
  433. LPBYTE buffer;
  434. UINT32 frames;
  435. ComPtr<IAudioClient> client;
  436. res = device->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr,
  437. (void **)client.Assign());
  438. if (FAILED(res))
  439. throw HRError("Failed to activate client context", res);
  440. res = client->GetMixFormat(&wfex);
  441. if (FAILED(res))
  442. throw HRError("Failed to get mix format", res);
  443. res = client->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, BUFFER_TIME_100NS,
  444. 0, wfex, nullptr);
  445. if (FAILED(res))
  446. throw HRError("Failed to initialize audio client", res);
  447. /* Silent loopback fix. Prevents audio stream from stopping and */
  448. /* messing up timestamps and other weird glitches during silence */
  449. /* by playing a silent sample all over again. */
  450. res = client->GetBufferSize(&frames);
  451. if (FAILED(res))
  452. throw HRError("Failed to get buffer size", res);
  453. ComPtr<IAudioRenderClient> render;
  454. res = client->GetService(IID_PPV_ARGS(render.Assign()));
  455. if (FAILED(res))
  456. throw HRError("Failed to get render client", res);
  457. res = render->GetBuffer(frames, &buffer);
  458. if (FAILED(res))
  459. throw HRError("Failed to get buffer", res);
  460. memset(buffer, 0, (size_t)frames * (size_t)wfex->nBlockAlign);
  461. render->ReleaseBuffer(frames, 0);
  462. }
  463. static speaker_layout ConvertSpeakerLayout(DWORD layout, WORD channels)
  464. {
  465. switch (layout) {
  466. case KSAUDIO_SPEAKER_2POINT1:
  467. return SPEAKERS_2POINT1;
  468. case KSAUDIO_SPEAKER_SURROUND:
  469. return SPEAKERS_4POINT0;
  470. case OBS_KSAUDIO_SPEAKER_4POINT1:
  471. return SPEAKERS_4POINT1;
  472. case KSAUDIO_SPEAKER_5POINT1_SURROUND:
  473. return SPEAKERS_5POINT1;
  474. case KSAUDIO_SPEAKER_7POINT1_SURROUND:
  475. return SPEAKERS_7POINT1;
  476. }
  477. return (speaker_layout)channels;
  478. }
  479. void WASAPISource::InitFormat(const WAVEFORMATEX *wfex,
  480. enum speaker_layout &speakers,
  481. enum audio_format &format, uint32_t &sampleRate)
  482. {
  483. DWORD layout = 0;
  484. if (wfex->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
  485. WAVEFORMATEXTENSIBLE *ext = (WAVEFORMATEXTENSIBLE *)wfex;
  486. layout = ext->dwChannelMask;
  487. }
  488. /* WASAPI is always float */
  489. speakers = ConvertSpeakerLayout(layout, wfex->nChannels);
  490. format = AUDIO_FORMAT_FLOAT;
  491. sampleRate = wfex->nSamplesPerSec;
  492. }
  493. ComPtr<IAudioCaptureClient> WASAPISource::InitCapture(IAudioClient *client,
  494. HANDLE receiveSignal)
  495. {
  496. ComPtr<IAudioCaptureClient> capture;
  497. HRESULT res = client->GetService(IID_PPV_ARGS(capture.Assign()));
  498. if (FAILED(res))
  499. throw HRError("Failed to create capture context", res);
  500. res = client->SetEventHandle(receiveSignal);
  501. if (FAILED(res))
  502. throw HRError("Failed to set event handle", res);
  503. res = client->Start();
  504. if (FAILED(res))
  505. throw HRError("Failed to start capture client", res);
  506. return capture;
  507. }
  508. void WASAPISource::Initialize()
  509. {
  510. ComPtr<IMMDevice> device = InitDevice(enumerator, isDefaultDevice,
  511. isInputDevice, device_id);
  512. device_name = GetDeviceName(device);
  513. ResetEvent(receiveSignal);
  514. ComPtr<IAudioClient> temp_client =
  515. InitClient(device, isInputDevice, speakers, format, sampleRate);
  516. if (!isInputDevice)
  517. ClearBuffer(device);
  518. ComPtr<IAudioCaptureClient> temp_capture =
  519. InitCapture(temp_client, receiveSignal);
  520. client = std::move(temp_client);
  521. capture = std::move(temp_capture);
  522. if (rtwq_supported) {
  523. HRESULT hr = rtwq_put_waiting_work_item(
  524. receiveSignal, 0, sampleReadyAsyncResult, nullptr);
  525. if (FAILED(hr)) {
  526. capture.Clear();
  527. client.Clear();
  528. throw HRError("RtwqPutWaitingWorkItem failed", hr);
  529. }
  530. hr = rtwq_put_waiting_work_item(restartSignal, 0,
  531. restartAsyncResult, nullptr);
  532. if (FAILED(hr)) {
  533. capture.Clear();
  534. client.Clear();
  535. throw HRError("RtwqPutWaitingWorkItem failed", hr);
  536. }
  537. }
  538. blog(LOG_INFO, "WASAPI: Device '%s' [%" PRIu32 " Hz] initialized",
  539. device_name.c_str(), sampleRate);
  540. }
  541. bool WASAPISource::TryInitialize()
  542. {
  543. bool success = false;
  544. try {
  545. Initialize();
  546. success = true;
  547. } catch (HRError &error) {
  548. if (!previouslyFailed) {
  549. blog(LOG_WARNING,
  550. "[WASAPISource::TryInitialize]:[%s] %s: %lX",
  551. device_name.empty() ? device_id.c_str()
  552. : device_name.c_str(),
  553. error.str, error.hr);
  554. }
  555. } catch (const char *error) {
  556. if (!previouslyFailed) {
  557. blog(LOG_WARNING,
  558. "[WASAPISource::TryInitialize]:[%s] %s",
  559. device_name.empty() ? device_id.c_str()
  560. : device_name.c_str(),
  561. error);
  562. }
  563. }
  564. previouslyFailed = !success;
  565. return success;
  566. }
  567. DWORD WINAPI WASAPISource::ReconnectThread(LPVOID param)
  568. {
  569. os_set_thread_name("win-wasapi: reconnect thread");
  570. WASAPISource *source = (WASAPISource *)param;
  571. const HANDLE sigs[] = {
  572. source->exitSignal,
  573. source->reconnectSignal,
  574. };
  575. bool exit = false;
  576. while (!exit) {
  577. const DWORD ret = WaitForMultipleObjects(_countof(sigs), sigs,
  578. false, INFINITE);
  579. switch (ret) {
  580. case WAIT_OBJECT_0:
  581. exit = true;
  582. break;
  583. default:
  584. assert(ret == (WAIT_OBJECT_0 + 1));
  585. if (source->reconnectDuration > 0) {
  586. WaitForSingleObject(source->stopSignal,
  587. source->reconnectDuration);
  588. }
  589. source->Start();
  590. }
  591. }
  592. return 0;
  593. }
  594. bool WASAPISource::ProcessCaptureData()
  595. {
  596. HRESULT res;
  597. LPBYTE buffer;
  598. UINT32 frames;
  599. DWORD flags;
  600. UINT64 pos, ts;
  601. UINT captureSize = 0;
  602. while (true) {
  603. res = capture->GetNextPacketSize(&captureSize);
  604. if (FAILED(res)) {
  605. if (res != AUDCLNT_E_DEVICE_INVALIDATED)
  606. blog(LOG_WARNING,
  607. "[WASAPISource::ProcessCaptureData]"
  608. " capture->GetNextPacketSize"
  609. " failed: %lX",
  610. res);
  611. return false;
  612. }
  613. if (!captureSize)
  614. break;
  615. res = capture->GetBuffer(&buffer, &frames, &flags, &pos, &ts);
  616. if (FAILED(res)) {
  617. if (res != AUDCLNT_E_DEVICE_INVALIDATED)
  618. blog(LOG_WARNING,
  619. "[WASAPISource::ProcessCaptureData]"
  620. " capture->GetBuffer"
  621. " failed: %lX",
  622. res);
  623. return false;
  624. }
  625. obs_source_audio data = {};
  626. data.data[0] = (const uint8_t *)buffer;
  627. data.frames = (uint32_t)frames;
  628. data.speakers = speakers;
  629. data.samples_per_sec = sampleRate;
  630. data.format = format;
  631. data.timestamp = useDeviceTiming ? ts * 100 : os_gettime_ns();
  632. if (!useDeviceTiming)
  633. data.timestamp -= util_mul_div64(frames, 1000000000ULL,
  634. sampleRate);
  635. obs_source_output_audio(source, &data);
  636. capture->ReleaseBuffer(frames);
  637. }
  638. return true;
  639. }
  640. #define RECONNECT_INTERVAL 3000
  641. DWORD WINAPI WASAPISource::CaptureThread(LPVOID param)
  642. {
  643. os_set_thread_name("win-wasapi: capture thread");
  644. const HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
  645. const bool com_initialized = SUCCEEDED(hr);
  646. if (!com_initialized) {
  647. blog(LOG_ERROR,
  648. "[WASAPISource::CaptureThread]"
  649. " CoInitializeEx failed: 0x%08X",
  650. hr);
  651. }
  652. DWORD unused = 0;
  653. const HANDLE handle = AvSetMmThreadCharacteristics(L"Audio", &unused);
  654. WASAPISource *source = (WASAPISource *)param;
  655. const HANDLE inactive_sigs[] = {
  656. source->exitSignal,
  657. source->stopSignal,
  658. source->initSignal,
  659. };
  660. const HANDLE active_sigs[] = {
  661. source->exitSignal,
  662. source->stopSignal,
  663. source->receiveSignal,
  664. source->restartSignal,
  665. };
  666. DWORD sig_count = _countof(inactive_sigs);
  667. const HANDLE *sigs = inactive_sigs;
  668. bool exit = false;
  669. while (!exit) {
  670. bool idle = false;
  671. bool stop = false;
  672. bool reconnect = false;
  673. do {
  674. /* Windows 7 does not seem to wake up for LOOPBACK */
  675. const DWORD dwMilliseconds = ((sigs == active_sigs) &&
  676. !source->isInputDevice)
  677. ? 10
  678. : INFINITE;
  679. const DWORD ret = WaitForMultipleObjects(
  680. sig_count, sigs, false, dwMilliseconds);
  681. switch (ret) {
  682. case WAIT_OBJECT_0: {
  683. exit = true;
  684. stop = true;
  685. idle = true;
  686. break;
  687. }
  688. case WAIT_OBJECT_0 + 1:
  689. stop = true;
  690. idle = true;
  691. break;
  692. case WAIT_OBJECT_0 + 2:
  693. case WAIT_TIMEOUT:
  694. if (sigs == inactive_sigs) {
  695. assert(ret != WAIT_TIMEOUT);
  696. if (source->TryInitialize()) {
  697. sig_count =
  698. _countof(active_sigs);
  699. sigs = active_sigs;
  700. } else {
  701. blog(LOG_INFO,
  702. "WASAPI: Device '%s' failed to start",
  703. source->device_id.c_str());
  704. stop = true;
  705. reconnect = true;
  706. source->reconnectDuration =
  707. RECONNECT_INTERVAL;
  708. }
  709. } else {
  710. stop = !source->ProcessCaptureData();
  711. if (stop) {
  712. blog(LOG_INFO,
  713. "Device '%s' invalidated. Retrying",
  714. source->device_name
  715. .c_str());
  716. stop = true;
  717. reconnect = true;
  718. source->reconnectDuration =
  719. RECONNECT_INTERVAL;
  720. }
  721. }
  722. break;
  723. default:
  724. assert(sigs == active_sigs);
  725. assert(ret == WAIT_OBJECT_0 + 3);
  726. stop = true;
  727. reconnect = true;
  728. source->reconnectDuration = 0;
  729. ResetEvent(source->restartSignal);
  730. }
  731. } while (!stop);
  732. sig_count = _countof(inactive_sigs);
  733. sigs = inactive_sigs;
  734. if (source->client) {
  735. source->client->Stop();
  736. source->capture.Clear();
  737. source->client.Clear();
  738. }
  739. if (idle) {
  740. SetEvent(source->idleSignal);
  741. } else if (reconnect) {
  742. blog(LOG_INFO, "Device '%s' invalidated. Retrying",
  743. source->device_name.c_str());
  744. SetEvent(source->reconnectSignal);
  745. }
  746. }
  747. if (handle)
  748. AvRevertMmThreadCharacteristics(handle);
  749. if (com_initialized)
  750. CoUninitialize();
  751. return 0;
  752. }
  753. void WASAPISource::SetDefaultDevice(EDataFlow flow, ERole role, LPCWSTR id)
  754. {
  755. if (!isDefaultDevice)
  756. return;
  757. const EDataFlow expectedFlow = isInputDevice ? eCapture : eRender;
  758. const ERole expectedRole = isInputDevice ? eCommunications : eConsole;
  759. if (flow != expectedFlow || role != expectedRole)
  760. return;
  761. if (id) {
  762. if (default_id.compare(id) == 0)
  763. return;
  764. default_id = id;
  765. } else {
  766. if (default_id.empty())
  767. return;
  768. default_id.clear();
  769. }
  770. blog(LOG_INFO, "WASAPI: Default %s device changed",
  771. isInputDevice ? "input" : "output");
  772. SetEvent(restartSignal);
  773. }
  774. void WASAPISource::OnStartCapture()
  775. {
  776. const DWORD ret = WaitForSingleObject(stopSignal, 0);
  777. switch (ret) {
  778. case WAIT_OBJECT_0:
  779. SetEvent(idleSignal);
  780. break;
  781. default:
  782. assert(ret == WAIT_TIMEOUT);
  783. if (!TryInitialize()) {
  784. blog(LOG_INFO, "WASAPI: Device '%s' failed to start",
  785. device_id.c_str());
  786. reconnectDuration = RECONNECT_INTERVAL;
  787. SetEvent(reconnectSignal);
  788. }
  789. }
  790. }
  791. void WASAPISource::OnSampleReady()
  792. {
  793. bool stop = false;
  794. bool reconnect = false;
  795. if (!ProcessCaptureData()) {
  796. stop = true;
  797. reconnect = true;
  798. reconnectDuration = RECONNECT_INTERVAL;
  799. }
  800. if (WaitForSingleObject(restartSignal, 0) == WAIT_OBJECT_0) {
  801. stop = true;
  802. reconnect = true;
  803. reconnectDuration = 0;
  804. ResetEvent(restartSignal);
  805. rtwq_put_waiting_work_item(restartSignal, 0, restartAsyncResult,
  806. nullptr);
  807. }
  808. if (WaitForSingleObject(stopSignal, 0) == WAIT_OBJECT_0) {
  809. stop = true;
  810. reconnect = false;
  811. }
  812. if (!stop) {
  813. if (FAILED(rtwq_put_waiting_work_item(receiveSignal, 0,
  814. sampleReadyAsyncResult,
  815. nullptr))) {
  816. blog(LOG_ERROR,
  817. "Could not requeue sample receive work");
  818. stop = true;
  819. reconnect = true;
  820. reconnectDuration = RECONNECT_INTERVAL;
  821. }
  822. }
  823. if (stop) {
  824. client->Stop();
  825. capture.Clear();
  826. client.Clear();
  827. if (reconnect) {
  828. blog(LOG_INFO, "Device '%s' invalidated. Retrying",
  829. device_name.c_str());
  830. SetEvent(reconnectSignal);
  831. } else {
  832. SetEvent(idleSignal);
  833. }
  834. }
  835. }
  836. void WASAPISource::OnRestart()
  837. {
  838. SetEvent(receiveSignal);
  839. }
  840. /* ------------------------------------------------------------------------- */
  841. static const char *GetWASAPIInputName(void *)
  842. {
  843. return obs_module_text("AudioInput");
  844. }
  845. static const char *GetWASAPIOutputName(void *)
  846. {
  847. return obs_module_text("AudioOutput");
  848. }
  849. static void GetWASAPIDefaultsInput(obs_data_t *settings)
  850. {
  851. obs_data_set_default_string(settings, OPT_DEVICE_ID, "default");
  852. obs_data_set_default_bool(settings, OPT_USE_DEVICE_TIMING, false);
  853. }
  854. static void GetWASAPIDefaultsOutput(obs_data_t *settings)
  855. {
  856. obs_data_set_default_string(settings, OPT_DEVICE_ID, "default");
  857. obs_data_set_default_bool(settings, OPT_USE_DEVICE_TIMING, true);
  858. }
  859. static void *CreateWASAPISource(obs_data_t *settings, obs_source_t *source,
  860. bool input)
  861. {
  862. try {
  863. return new WASAPISource(settings, source, input);
  864. } catch (const char *error) {
  865. blog(LOG_ERROR, "[CreateWASAPISource] %s", error);
  866. }
  867. return nullptr;
  868. }
  869. static void *CreateWASAPIInput(obs_data_t *settings, obs_source_t *source)
  870. {
  871. return CreateWASAPISource(settings, source, true);
  872. }
  873. static void *CreateWASAPIOutput(obs_data_t *settings, obs_source_t *source)
  874. {
  875. return CreateWASAPISource(settings, source, false);
  876. }
  877. static void DestroyWASAPISource(void *obj)
  878. {
  879. delete static_cast<WASAPISource *>(obj);
  880. }
  881. static void UpdateWASAPISource(void *obj, obs_data_t *settings)
  882. {
  883. static_cast<WASAPISource *>(obj)->Update(settings);
  884. }
  885. static obs_properties_t *GetWASAPIProperties(bool input)
  886. {
  887. obs_properties_t *props = obs_properties_create();
  888. vector<AudioDeviceInfo> devices;
  889. obs_property_t *device_prop = obs_properties_add_list(
  890. props, OPT_DEVICE_ID, obs_module_text("Device"),
  891. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  892. GetWASAPIAudioDevices(devices, input);
  893. if (devices.size())
  894. obs_property_list_add_string(
  895. device_prop, obs_module_text("Default"), "default");
  896. for (size_t i = 0; i < devices.size(); i++) {
  897. AudioDeviceInfo &device = devices[i];
  898. obs_property_list_add_string(device_prop, device.name.c_str(),
  899. device.id.c_str());
  900. }
  901. obs_properties_add_bool(props, OPT_USE_DEVICE_TIMING,
  902. obs_module_text("UseDeviceTiming"));
  903. return props;
  904. }
  905. static obs_properties_t *GetWASAPIPropertiesInput(void *)
  906. {
  907. return GetWASAPIProperties(true);
  908. }
  909. static obs_properties_t *GetWASAPIPropertiesOutput(void *)
  910. {
  911. return GetWASAPIProperties(false);
  912. }
  913. void RegisterWASAPIInput()
  914. {
  915. obs_source_info info = {};
  916. info.id = "wasapi_input_capture";
  917. info.type = OBS_SOURCE_TYPE_INPUT;
  918. info.output_flags = OBS_SOURCE_AUDIO | OBS_SOURCE_DO_NOT_DUPLICATE;
  919. info.get_name = GetWASAPIInputName;
  920. info.create = CreateWASAPIInput;
  921. info.destroy = DestroyWASAPISource;
  922. info.update = UpdateWASAPISource;
  923. info.get_defaults = GetWASAPIDefaultsInput;
  924. info.get_properties = GetWASAPIPropertiesInput;
  925. info.icon_type = OBS_ICON_TYPE_AUDIO_INPUT;
  926. obs_register_source(&info);
  927. }
  928. void RegisterWASAPIOutput()
  929. {
  930. obs_source_info info = {};
  931. info.id = "wasapi_output_capture";
  932. info.type = OBS_SOURCE_TYPE_INPUT;
  933. info.output_flags = OBS_SOURCE_AUDIO | OBS_SOURCE_DO_NOT_DUPLICATE |
  934. OBS_SOURCE_DO_NOT_SELF_MONITOR;
  935. info.get_name = GetWASAPIOutputName;
  936. info.create = CreateWASAPIOutput;
  937. info.destroy = DestroyWASAPISource;
  938. info.update = UpdateWASAPISource;
  939. info.get_defaults = GetWASAPIDefaultsOutput;
  940. info.get_properties = GetWASAPIPropertiesOutput;
  941. info.icon_type = OBS_ICON_TYPE_AUDIO_OUTPUT;
  942. obs_register_source(&info);
  943. }