winmm.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2007 by authors.
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include "winmm.h"
  22. #include <cstdlib>
  23. #include <cstdio>
  24. #include <memory.h>
  25. #include <windows.h>
  26. #include <mmsystem.h>
  27. #include <mmreg.h>
  28. #include <array>
  29. #include <atomic>
  30. #include <thread>
  31. #include <vector>
  32. #include <string>
  33. #include <algorithm>
  34. #include <functional>
  35. #include "alsem.h"
  36. #include "alstring.h"
  37. #include "althrd_setname.h"
  38. #include "core/device.h"
  39. #include "core/helpers.h"
  40. #include "core/logging.h"
  41. #include "ringbuffer.h"
  42. #include "strutils.h"
  43. #include "vector.h"
  44. #ifndef WAVE_FORMAT_IEEE_FLOAT
  45. #define WAVE_FORMAT_IEEE_FLOAT 0x0003
  46. #endif
  47. namespace {
  48. #define DEVNAME_HEAD "OpenAL Soft on "
  49. std::vector<std::string> PlaybackDevices;
  50. std::vector<std::string> CaptureDevices;
  51. bool checkName(const std::vector<std::string> &list, const std::string &name)
  52. { return std::find(list.cbegin(), list.cend(), name) != list.cend(); }
  53. void ProbePlaybackDevices()
  54. {
  55. PlaybackDevices.clear();
  56. UINT numdevs{waveOutGetNumDevs()};
  57. PlaybackDevices.reserve(numdevs);
  58. for(UINT i{0};i < numdevs;++i)
  59. {
  60. std::string dname;
  61. WAVEOUTCAPSW WaveCaps{};
  62. if(waveOutGetDevCapsW(i, &WaveCaps, sizeof(WaveCaps)) == MMSYSERR_NOERROR)
  63. {
  64. const std::string basename{DEVNAME_HEAD + wstr_to_utf8(std::data(WaveCaps.szPname))};
  65. int count{1};
  66. std::string newname{basename};
  67. while(checkName(PlaybackDevices, newname))
  68. {
  69. newname = basename;
  70. newname += " #";
  71. newname += std::to_string(++count);
  72. }
  73. dname = std::move(newname);
  74. TRACE("Got device \"%s\", ID %u\n", dname.c_str(), i);
  75. }
  76. PlaybackDevices.emplace_back(std::move(dname));
  77. }
  78. }
  79. void ProbeCaptureDevices()
  80. {
  81. CaptureDevices.clear();
  82. UINT numdevs{waveInGetNumDevs()};
  83. CaptureDevices.reserve(numdevs);
  84. for(UINT i{0};i < numdevs;++i)
  85. {
  86. std::string dname;
  87. WAVEINCAPSW WaveCaps{};
  88. if(waveInGetDevCapsW(i, &WaveCaps, sizeof(WaveCaps)) == MMSYSERR_NOERROR)
  89. {
  90. const std::string basename{DEVNAME_HEAD + wstr_to_utf8(std::data(WaveCaps.szPname))};
  91. int count{1};
  92. std::string newname{basename};
  93. while(checkName(CaptureDevices, newname))
  94. {
  95. newname = basename;
  96. newname += " #";
  97. newname += std::to_string(++count);
  98. }
  99. dname = std::move(newname);
  100. TRACE("Got device \"%s\", ID %u\n", dname.c_str(), i);
  101. }
  102. CaptureDevices.emplace_back(std::move(dname));
  103. }
  104. }
  105. struct WinMMPlayback final : public BackendBase {
  106. WinMMPlayback(DeviceBase *device) noexcept : BackendBase{device} { }
  107. ~WinMMPlayback() override;
  108. void CALLBACK waveOutProc(HWAVEOUT device, UINT msg, DWORD_PTR param1, DWORD_PTR param2) noexcept;
  109. static void CALLBACK waveOutProcC(HWAVEOUT device, UINT msg, DWORD_PTR instance, DWORD_PTR param1, DWORD_PTR param2) noexcept
  110. { reinterpret_cast<WinMMPlayback*>(instance)->waveOutProc(device, msg, param1, param2); }
  111. int mixerProc();
  112. void open(std::string_view name) override;
  113. bool reset() override;
  114. void start() override;
  115. void stop() override;
  116. std::atomic<uint> mWritable{0u};
  117. al::semaphore mSem;
  118. uint mIdx{0u};
  119. std::array<WAVEHDR,4> mWaveBuffer{};
  120. al::vector<char,16> mBuffer;
  121. HWAVEOUT mOutHdl{nullptr};
  122. WAVEFORMATEX mFormat{};
  123. std::atomic<bool> mKillNow{true};
  124. std::thread mThread;
  125. };
  126. WinMMPlayback::~WinMMPlayback()
  127. {
  128. if(mOutHdl)
  129. waveOutClose(mOutHdl);
  130. mOutHdl = nullptr;
  131. }
  132. /* WinMMPlayback::waveOutProc
  133. *
  134. * Posts a message to 'WinMMPlayback::mixerProc' every time a WaveOut Buffer is
  135. * completed and returns to the application (for more data)
  136. */
  137. void CALLBACK WinMMPlayback::waveOutProc(HWAVEOUT, UINT msg, DWORD_PTR, DWORD_PTR) noexcept
  138. {
  139. if(msg != WOM_DONE) return;
  140. mWritable.fetch_add(1, std::memory_order_acq_rel);
  141. mSem.post();
  142. }
  143. FORCE_ALIGN int WinMMPlayback::mixerProc()
  144. {
  145. SetRTPriority();
  146. althrd_setname(GetMixerThreadName());
  147. while(!mKillNow.load(std::memory_order_acquire)
  148. && mDevice->Connected.load(std::memory_order_acquire))
  149. {
  150. uint todo{mWritable.load(std::memory_order_acquire)};
  151. if(todo < 1)
  152. {
  153. mSem.wait();
  154. continue;
  155. }
  156. size_t widx{mIdx};
  157. do {
  158. WAVEHDR &waveHdr = mWaveBuffer[widx];
  159. if(++widx == mWaveBuffer.size()) widx = 0;
  160. mDevice->renderSamples(waveHdr.lpData, mDevice->UpdateSize, mFormat.nChannels);
  161. mWritable.fetch_sub(1, std::memory_order_acq_rel);
  162. waveOutWrite(mOutHdl, &waveHdr, sizeof(WAVEHDR));
  163. } while(--todo);
  164. mIdx = static_cast<uint>(widx);
  165. }
  166. return 0;
  167. }
  168. void WinMMPlayback::open(std::string_view name)
  169. {
  170. if(PlaybackDevices.empty())
  171. ProbePlaybackDevices();
  172. // Find the Device ID matching the deviceName if valid
  173. auto iter = !name.empty() ?
  174. std::find(PlaybackDevices.cbegin(), PlaybackDevices.cend(), name) :
  175. PlaybackDevices.cbegin();
  176. if(iter == PlaybackDevices.cend())
  177. throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%.*s\" not found",
  178. al::sizei(name), name.data()};
  179. auto DeviceID = static_cast<UINT>(std::distance(PlaybackDevices.cbegin(), iter));
  180. DevFmtType fmttype{mDevice->FmtType};
  181. WAVEFORMATEX format{};
  182. do {
  183. format = WAVEFORMATEX{};
  184. if(fmttype == DevFmtFloat)
  185. {
  186. format.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
  187. format.wBitsPerSample = 32;
  188. }
  189. else
  190. {
  191. format.wFormatTag = WAVE_FORMAT_PCM;
  192. if(fmttype == DevFmtUByte || fmttype == DevFmtByte)
  193. format.wBitsPerSample = 8;
  194. else
  195. format.wBitsPerSample = 16;
  196. }
  197. format.nChannels = ((mDevice->FmtChans == DevFmtMono) ? 1 : 2);
  198. format.nBlockAlign = static_cast<WORD>(format.wBitsPerSample * format.nChannels / 8);
  199. format.nSamplesPerSec = mDevice->Frequency;
  200. format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
  201. format.cbSize = 0;
  202. MMRESULT res{waveOutOpen(&mOutHdl, DeviceID, &format,
  203. reinterpret_cast<DWORD_PTR>(&WinMMPlayback::waveOutProcC),
  204. reinterpret_cast<DWORD_PTR>(this), CALLBACK_FUNCTION)};
  205. if(res == MMSYSERR_NOERROR) break;
  206. if(fmttype != DevFmtFloat)
  207. throw al::backend_exception{al::backend_error::DeviceError, "waveOutOpen failed: %u",
  208. res};
  209. fmttype = DevFmtShort;
  210. } while(true);
  211. mFormat = format;
  212. mDevice->DeviceName = PlaybackDevices[DeviceID];
  213. }
  214. bool WinMMPlayback::reset()
  215. {
  216. mDevice->BufferSize = static_cast<uint>(uint64_t{mDevice->BufferSize} *
  217. mFormat.nSamplesPerSec / mDevice->Frequency);
  218. mDevice->BufferSize = (mDevice->BufferSize+3) & ~0x3u;
  219. mDevice->UpdateSize = mDevice->BufferSize / 4;
  220. mDevice->Frequency = mFormat.nSamplesPerSec;
  221. if(mFormat.wFormatTag == WAVE_FORMAT_IEEE_FLOAT)
  222. {
  223. if(mFormat.wBitsPerSample == 32)
  224. mDevice->FmtType = DevFmtFloat;
  225. else
  226. {
  227. ERR("Unhandled IEEE float sample depth: %d\n", mFormat.wBitsPerSample);
  228. return false;
  229. }
  230. }
  231. else if(mFormat.wFormatTag == WAVE_FORMAT_PCM)
  232. {
  233. if(mFormat.wBitsPerSample == 16)
  234. mDevice->FmtType = DevFmtShort;
  235. else if(mFormat.wBitsPerSample == 8)
  236. mDevice->FmtType = DevFmtUByte;
  237. else
  238. {
  239. ERR("Unhandled PCM sample depth: %d\n", mFormat.wBitsPerSample);
  240. return false;
  241. }
  242. }
  243. else
  244. {
  245. ERR("Unhandled format tag: 0x%04x\n", mFormat.wFormatTag);
  246. return false;
  247. }
  248. if(mFormat.nChannels >= 2)
  249. mDevice->FmtChans = DevFmtStereo;
  250. else if(mFormat.nChannels == 1)
  251. mDevice->FmtChans = DevFmtMono;
  252. else
  253. {
  254. ERR("Unhandled channel count: %d\n", mFormat.nChannels);
  255. return false;
  256. }
  257. setDefaultWFXChannelOrder();
  258. const uint BufferSize{mDevice->UpdateSize * mFormat.nChannels * mDevice->bytesFromFmt()};
  259. decltype(mBuffer)(BufferSize*mWaveBuffer.size()).swap(mBuffer);
  260. mWaveBuffer[0] = WAVEHDR{};
  261. mWaveBuffer[0].lpData = mBuffer.data();
  262. mWaveBuffer[0].dwBufferLength = BufferSize;
  263. for(size_t i{1};i < mWaveBuffer.size();i++)
  264. {
  265. mWaveBuffer[i] = WAVEHDR{};
  266. mWaveBuffer[i].lpData = mWaveBuffer[i-1].lpData + mWaveBuffer[i-1].dwBufferLength;
  267. mWaveBuffer[i].dwBufferLength = BufferSize;
  268. }
  269. mIdx = 0;
  270. return true;
  271. }
  272. void WinMMPlayback::start()
  273. {
  274. try {
  275. for(auto &waveHdr : mWaveBuffer)
  276. waveOutPrepareHeader(mOutHdl, &waveHdr, sizeof(WAVEHDR));
  277. mWritable.store(static_cast<uint>(mWaveBuffer.size()), std::memory_order_release);
  278. mKillNow.store(false, std::memory_order_release);
  279. mThread = std::thread{std::mem_fn(&WinMMPlayback::mixerProc), this};
  280. }
  281. catch(std::exception& e) {
  282. throw al::backend_exception{al::backend_error::DeviceError,
  283. "Failed to start mixing thread: %s", e.what()};
  284. }
  285. }
  286. void WinMMPlayback::stop()
  287. {
  288. if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
  289. return;
  290. mThread.join();
  291. while(mWritable.load(std::memory_order_acquire) < mWaveBuffer.size())
  292. mSem.wait();
  293. for(auto &waveHdr : mWaveBuffer)
  294. waveOutUnprepareHeader(mOutHdl, &waveHdr, sizeof(WAVEHDR));
  295. mWritable.store(0, std::memory_order_release);
  296. }
  297. struct WinMMCapture final : public BackendBase {
  298. WinMMCapture(DeviceBase *device) noexcept : BackendBase{device} { }
  299. ~WinMMCapture() override;
  300. void CALLBACK waveInProc(HWAVEIN device, UINT msg, DWORD_PTR param1, DWORD_PTR param2) noexcept;
  301. static void CALLBACK waveInProcC(HWAVEIN device, UINT msg, DWORD_PTR instance, DWORD_PTR param1, DWORD_PTR param2) noexcept
  302. { reinterpret_cast<WinMMCapture*>(instance)->waveInProc(device, msg, param1, param2); }
  303. int captureProc();
  304. void open(std::string_view name) override;
  305. void start() override;
  306. void stop() override;
  307. void captureSamples(std::byte *buffer, uint samples) override;
  308. uint availableSamples() override;
  309. std::atomic<uint> mReadable{0u};
  310. al::semaphore mSem;
  311. uint mIdx{0};
  312. std::array<WAVEHDR,4> mWaveBuffer{};
  313. al::vector<char,16> mBuffer;
  314. HWAVEIN mInHdl{nullptr};
  315. RingBufferPtr mRing{nullptr};
  316. WAVEFORMATEX mFormat{};
  317. std::atomic<bool> mKillNow{true};
  318. std::thread mThread;
  319. };
  320. WinMMCapture::~WinMMCapture()
  321. {
  322. // Close the Wave device
  323. if(mInHdl)
  324. waveInClose(mInHdl);
  325. mInHdl = nullptr;
  326. }
  327. /* WinMMCapture::waveInProc
  328. *
  329. * Posts a message to 'WinMMCapture::captureProc' every time a WaveIn Buffer is
  330. * completed and returns to the application (with more data).
  331. */
  332. void CALLBACK WinMMCapture::waveInProc(HWAVEIN, UINT msg, DWORD_PTR, DWORD_PTR) noexcept
  333. {
  334. if(msg != WIM_DATA) return;
  335. mReadable.fetch_add(1, std::memory_order_acq_rel);
  336. mSem.post();
  337. }
  338. int WinMMCapture::captureProc()
  339. {
  340. althrd_setname(GetRecordThreadName());
  341. while(!mKillNow.load(std::memory_order_acquire) &&
  342. mDevice->Connected.load(std::memory_order_acquire))
  343. {
  344. uint todo{mReadable.load(std::memory_order_acquire)};
  345. if(todo < 1)
  346. {
  347. mSem.wait();
  348. continue;
  349. }
  350. size_t widx{mIdx};
  351. do {
  352. WAVEHDR &waveHdr = mWaveBuffer[widx];
  353. widx = (widx+1) % mWaveBuffer.size();
  354. std::ignore = mRing->write(waveHdr.lpData,
  355. waveHdr.dwBytesRecorded / mFormat.nBlockAlign);
  356. mReadable.fetch_sub(1, std::memory_order_acq_rel);
  357. waveInAddBuffer(mInHdl, &waveHdr, sizeof(WAVEHDR));
  358. } while(--todo);
  359. mIdx = static_cast<uint>(widx);
  360. }
  361. return 0;
  362. }
  363. void WinMMCapture::open(std::string_view name)
  364. {
  365. if(CaptureDevices.empty())
  366. ProbeCaptureDevices();
  367. // Find the Device ID matching the deviceName if valid
  368. auto iter = !name.empty() ?
  369. std::find(CaptureDevices.cbegin(), CaptureDevices.cend(), name) :
  370. CaptureDevices.cbegin();
  371. if(iter == CaptureDevices.cend())
  372. throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%.*s\" not found",
  373. al::sizei(name), name.data()};
  374. auto DeviceID = static_cast<UINT>(std::distance(CaptureDevices.cbegin(), iter));
  375. switch(mDevice->FmtChans)
  376. {
  377. case DevFmtMono:
  378. case DevFmtStereo:
  379. break;
  380. case DevFmtQuad:
  381. case DevFmtX51:
  382. case DevFmtX61:
  383. case DevFmtX71:
  384. case DevFmtX714:
  385. case DevFmtX7144:
  386. case DevFmtX3D71:
  387. case DevFmtAmbi3D:
  388. throw al::backend_exception{al::backend_error::DeviceError, "%s capture not supported",
  389. DevFmtChannelsString(mDevice->FmtChans)};
  390. }
  391. switch(mDevice->FmtType)
  392. {
  393. case DevFmtUByte:
  394. case DevFmtShort:
  395. case DevFmtInt:
  396. case DevFmtFloat:
  397. break;
  398. case DevFmtByte:
  399. case DevFmtUShort:
  400. case DevFmtUInt:
  401. throw al::backend_exception{al::backend_error::DeviceError, "%s samples not supported",
  402. DevFmtTypeString(mDevice->FmtType)};
  403. }
  404. mFormat = WAVEFORMATEX{};
  405. mFormat.wFormatTag = (mDevice->FmtType == DevFmtFloat) ?
  406. WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM;
  407. mFormat.nChannels = static_cast<WORD>(mDevice->channelsFromFmt());
  408. mFormat.wBitsPerSample = static_cast<WORD>(mDevice->bytesFromFmt() * 8);
  409. mFormat.nBlockAlign = static_cast<WORD>(mFormat.wBitsPerSample * mFormat.nChannels / 8);
  410. mFormat.nSamplesPerSec = mDevice->Frequency;
  411. mFormat.nAvgBytesPerSec = mFormat.nSamplesPerSec * mFormat.nBlockAlign;
  412. mFormat.cbSize = 0;
  413. MMRESULT res{waveInOpen(&mInHdl, DeviceID, &mFormat,
  414. reinterpret_cast<DWORD_PTR>(&WinMMCapture::waveInProcC),
  415. reinterpret_cast<DWORD_PTR>(this), CALLBACK_FUNCTION)};
  416. if(res != MMSYSERR_NOERROR)
  417. throw al::backend_exception{al::backend_error::DeviceError, "waveInOpen failed: %u", res};
  418. // Ensure each buffer is 50ms each
  419. DWORD BufferSize{mFormat.nAvgBytesPerSec / 20u};
  420. BufferSize -= (BufferSize % mFormat.nBlockAlign);
  421. // Allocate circular memory buffer for the captured audio
  422. // Make sure circular buffer is at least 100ms in size
  423. const auto CapturedDataSize = std::max<size_t>(mDevice->BufferSize,
  424. BufferSize*mWaveBuffer.size());
  425. mRing = RingBuffer::Create(CapturedDataSize, mFormat.nBlockAlign, false);
  426. decltype(mBuffer)(BufferSize*mWaveBuffer.size()).swap(mBuffer);
  427. mWaveBuffer[0] = WAVEHDR{};
  428. mWaveBuffer[0].lpData = mBuffer.data();
  429. mWaveBuffer[0].dwBufferLength = BufferSize;
  430. for(size_t i{1};i < mWaveBuffer.size();++i)
  431. {
  432. mWaveBuffer[i] = WAVEHDR{};
  433. mWaveBuffer[i].lpData = mWaveBuffer[i-1].lpData + mWaveBuffer[i-1].dwBufferLength;
  434. mWaveBuffer[i].dwBufferLength = mWaveBuffer[i-1].dwBufferLength;
  435. }
  436. mDevice->DeviceName = CaptureDevices[DeviceID];
  437. }
  438. void WinMMCapture::start()
  439. {
  440. try {
  441. for(size_t i{0};i < mWaveBuffer.size();++i)
  442. {
  443. waveInPrepareHeader(mInHdl, &mWaveBuffer[i], sizeof(WAVEHDR));
  444. waveInAddBuffer(mInHdl, &mWaveBuffer[i], sizeof(WAVEHDR));
  445. }
  446. mKillNow.store(false, std::memory_order_release);
  447. mThread = std::thread{std::mem_fn(&WinMMCapture::captureProc), this};
  448. waveInStart(mInHdl);
  449. }
  450. catch(std::exception& e) {
  451. throw al::backend_exception{al::backend_error::DeviceError,
  452. "Failed to start recording thread: %s", e.what()};
  453. }
  454. }
  455. void WinMMCapture::stop()
  456. {
  457. waveInStop(mInHdl);
  458. mKillNow.store(true, std::memory_order_release);
  459. if(mThread.joinable())
  460. {
  461. mSem.post();
  462. mThread.join();
  463. }
  464. waveInReset(mInHdl);
  465. for(size_t i{0};i < mWaveBuffer.size();++i)
  466. waveInUnprepareHeader(mInHdl, &mWaveBuffer[i], sizeof(WAVEHDR));
  467. mReadable.store(0, std::memory_order_release);
  468. mIdx = 0;
  469. }
  470. void WinMMCapture::captureSamples(std::byte *buffer, uint samples)
  471. { std::ignore = mRing->read(buffer, samples); }
  472. uint WinMMCapture::availableSamples()
  473. { return static_cast<uint>(mRing->readSpace()); }
  474. } // namespace
  475. bool WinMMBackendFactory::init()
  476. { return true; }
  477. bool WinMMBackendFactory::querySupport(BackendType type)
  478. { return type == BackendType::Playback || type == BackendType::Capture; }
  479. auto WinMMBackendFactory::enumerate(BackendType type) -> std::vector<std::string>
  480. {
  481. std::vector<std::string> outnames;
  482. auto add_device = [&outnames](const std::string &dname) -> void
  483. { if(!dname.empty()) outnames.emplace_back(dname); };
  484. switch(type)
  485. {
  486. case BackendType::Playback:
  487. ProbePlaybackDevices();
  488. outnames.reserve(PlaybackDevices.size());
  489. std::for_each(PlaybackDevices.cbegin(), PlaybackDevices.cend(), add_device);
  490. break;
  491. case BackendType::Capture:
  492. ProbeCaptureDevices();
  493. outnames.reserve(CaptureDevices.size());
  494. std::for_each(CaptureDevices.cbegin(), CaptureDevices.cend(), add_device);
  495. break;
  496. }
  497. return outnames;
  498. }
  499. BackendPtr WinMMBackendFactory::createBackend(DeviceBase *device, BackendType type)
  500. {
  501. if(type == BackendType::Playback)
  502. return BackendPtr{new WinMMPlayback{device}};
  503. if(type == BackendType::Capture)
  504. return BackendPtr{new WinMMCapture{device}};
  505. return nullptr;
  506. }
  507. BackendFactory &WinMMBackendFactory::getFactory()
  508. {
  509. static WinMMBackendFactory factory{};
  510. return factory;
  511. }