win-wasapi.cpp 28 KB

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