winmm.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 "backends/winmm.h"
  22. #include <stdlib.h>
  23. #include <stdio.h>
  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 "alcmain.h"
  36. #include "alu.h"
  37. #include "compat.h"
  38. #include "core/logging.h"
  39. #include "ringbuffer.h"
  40. #include "strutils.h"
  41. #include "threads.h"
  42. #ifndef WAVE_FORMAT_IEEE_FLOAT
  43. #define WAVE_FORMAT_IEEE_FLOAT 0x0003
  44. #endif
  45. namespace {
  46. #define DEVNAME_HEAD "OpenAL Soft on "
  47. al::vector<std::string> PlaybackDevices;
  48. al::vector<std::string> CaptureDevices;
  49. bool checkName(const al::vector<std::string> &list, const std::string &name)
  50. { return std::find(list.cbegin(), list.cend(), name) != list.cend(); }
  51. void ProbePlaybackDevices(void)
  52. {
  53. PlaybackDevices.clear();
  54. UINT numdevs{waveOutGetNumDevs()};
  55. PlaybackDevices.reserve(numdevs);
  56. for(UINT i{0};i < numdevs;++i)
  57. {
  58. std::string dname;
  59. WAVEOUTCAPSW WaveCaps{};
  60. if(waveOutGetDevCapsW(i, &WaveCaps, sizeof(WaveCaps)) == MMSYSERR_NOERROR)
  61. {
  62. const std::string basename{DEVNAME_HEAD + wstr_to_utf8(WaveCaps.szPname)};
  63. int count{1};
  64. std::string newname{basename};
  65. while(checkName(PlaybackDevices, newname))
  66. {
  67. newname = basename;
  68. newname += " #";
  69. newname += std::to_string(++count);
  70. }
  71. dname = std::move(newname);
  72. TRACE("Got device \"%s\", ID %u\n", dname.c_str(), i);
  73. }
  74. PlaybackDevices.emplace_back(std::move(dname));
  75. }
  76. }
  77. void ProbeCaptureDevices(void)
  78. {
  79. CaptureDevices.clear();
  80. UINT numdevs{waveInGetNumDevs()};
  81. CaptureDevices.reserve(numdevs);
  82. for(UINT i{0};i < numdevs;++i)
  83. {
  84. std::string dname;
  85. WAVEINCAPSW WaveCaps{};
  86. if(waveInGetDevCapsW(i, &WaveCaps, sizeof(WaveCaps)) == MMSYSERR_NOERROR)
  87. {
  88. const std::string basename{DEVNAME_HEAD + wstr_to_utf8(WaveCaps.szPname)};
  89. int count{1};
  90. std::string newname{basename};
  91. while(checkName(CaptureDevices, newname))
  92. {
  93. newname = basename;
  94. newname += " #";
  95. newname += std::to_string(++count);
  96. }
  97. dname = std::move(newname);
  98. TRACE("Got device \"%s\", ID %u\n", dname.c_str(), i);
  99. }
  100. CaptureDevices.emplace_back(std::move(dname));
  101. }
  102. }
  103. struct WinMMPlayback final : public BackendBase {
  104. WinMMPlayback(ALCdevice *device) noexcept : BackendBase{device} { }
  105. ~WinMMPlayback() override;
  106. void CALLBACK waveOutProc(HWAVEOUT device, UINT msg, DWORD_PTR param1, DWORD_PTR param2) noexcept;
  107. static void CALLBACK waveOutProcC(HWAVEOUT device, UINT msg, DWORD_PTR instance, DWORD_PTR param1, DWORD_PTR param2) noexcept
  108. { reinterpret_cast<WinMMPlayback*>(instance)->waveOutProc(device, msg, param1, param2); }
  109. int mixerProc();
  110. void open(const char *name) override;
  111. bool reset() override;
  112. void start() override;
  113. void stop() override;
  114. std::atomic<uint> mWritable{0u};
  115. al::semaphore mSem;
  116. uint mIdx{0u};
  117. std::array<WAVEHDR,4> mWaveBuffer{};
  118. HWAVEOUT mOutHdl{nullptr};
  119. WAVEFORMATEX mFormat{};
  120. std::atomic<bool> mKillNow{true};
  121. std::thread mThread;
  122. DEF_NEWDEL(WinMMPlayback)
  123. };
  124. WinMMPlayback::~WinMMPlayback()
  125. {
  126. if(mOutHdl)
  127. waveOutClose(mOutHdl);
  128. mOutHdl = nullptr;
  129. al_free(mWaveBuffer[0].lpData);
  130. std::fill(mWaveBuffer.begin(), mWaveBuffer.end(), WAVEHDR{});
  131. }
  132. /* WinMMPlayback::waveOutProc
  133. *
  134. * Posts a message to 'WinMMPlayback::mixerProc' everytime 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(MIXER_THREAD_NAME);
  147. const size_t frame_step{mDevice->channelsFromFmt()};
  148. while(!mKillNow.load(std::memory_order_acquire)
  149. && mDevice->Connected.load(std::memory_order_acquire))
  150. {
  151. uint todo{mWritable.load(std::memory_order_acquire)};
  152. if(todo < 1)
  153. {
  154. mSem.wait();
  155. continue;
  156. }
  157. size_t widx{mIdx};
  158. do {
  159. WAVEHDR &waveHdr = mWaveBuffer[widx];
  160. widx = (widx+1) % mWaveBuffer.size();
  161. mDevice->renderSamples(waveHdr.lpData, mDevice->UpdateSize, frame_step);
  162. mWritable.fetch_sub(1, std::memory_order_acq_rel);
  163. waveOutWrite(mOutHdl, &waveHdr, sizeof(WAVEHDR));
  164. } while(--todo);
  165. mIdx = static_cast<uint>(widx);
  166. }
  167. return 0;
  168. }
  169. void WinMMPlayback::open(const char *name)
  170. {
  171. if(PlaybackDevices.empty())
  172. ProbePlaybackDevices();
  173. // Find the Device ID matching the deviceName if valid
  174. auto iter = name ?
  175. std::find(PlaybackDevices.cbegin(), PlaybackDevices.cend(), name) :
  176. PlaybackDevices.cbegin();
  177. if(iter == PlaybackDevices.cend())
  178. throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%s\" not found",
  179. name};
  180. auto DeviceID = static_cast<UINT>(std::distance(PlaybackDevices.cbegin(), iter));
  181. retry_open:
  182. mFormat = WAVEFORMATEX{};
  183. if(mDevice->FmtType == DevFmtFloat)
  184. {
  185. mFormat.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
  186. mFormat.wBitsPerSample = 32;
  187. }
  188. else
  189. {
  190. mFormat.wFormatTag = WAVE_FORMAT_PCM;
  191. if(mDevice->FmtType == DevFmtUByte || mDevice->FmtType == DevFmtByte)
  192. mFormat.wBitsPerSample = 8;
  193. else
  194. mFormat.wBitsPerSample = 16;
  195. }
  196. mFormat.nChannels = ((mDevice->FmtChans == DevFmtMono) ? 1 : 2);
  197. mFormat.nBlockAlign = static_cast<WORD>(mFormat.wBitsPerSample * mFormat.nChannels / 8);
  198. mFormat.nSamplesPerSec = mDevice->Frequency;
  199. mFormat.nAvgBytesPerSec = mFormat.nSamplesPerSec * mFormat.nBlockAlign;
  200. mFormat.cbSize = 0;
  201. MMRESULT res{waveOutOpen(&mOutHdl, DeviceID, &mFormat,
  202. reinterpret_cast<DWORD_PTR>(&WinMMPlayback::waveOutProcC),
  203. reinterpret_cast<DWORD_PTR>(this), CALLBACK_FUNCTION)};
  204. if(res != MMSYSERR_NOERROR)
  205. {
  206. if(mDevice->FmtType == DevFmtFloat)
  207. {
  208. mDevice->FmtType = DevFmtShort;
  209. goto retry_open;
  210. }
  211. throw al::backend_exception{al::backend_error::DeviceError, "waveOutOpen failed: %u", res};
  212. }
  213. mDevice->DeviceName = PlaybackDevices[DeviceID];
  214. }
  215. bool WinMMPlayback::reset()
  216. {
  217. mDevice->BufferSize = static_cast<uint>(uint64_t{mDevice->BufferSize} *
  218. mFormat.nSamplesPerSec / mDevice->Frequency);
  219. mDevice->BufferSize = (mDevice->BufferSize+3) & ~0x3u;
  220. mDevice->UpdateSize = mDevice->BufferSize / 4;
  221. mDevice->Frequency = mFormat.nSamplesPerSec;
  222. if(mFormat.wFormatTag == WAVE_FORMAT_IEEE_FLOAT)
  223. {
  224. if(mFormat.wBitsPerSample == 32)
  225. mDevice->FmtType = DevFmtFloat;
  226. else
  227. {
  228. ERR("Unhandled IEEE float sample depth: %d\n", mFormat.wBitsPerSample);
  229. return false;
  230. }
  231. }
  232. else if(mFormat.wFormatTag == WAVE_FORMAT_PCM)
  233. {
  234. if(mFormat.wBitsPerSample == 16)
  235. mDevice->FmtType = DevFmtShort;
  236. else if(mFormat.wBitsPerSample == 8)
  237. mDevice->FmtType = DevFmtUByte;
  238. else
  239. {
  240. ERR("Unhandled PCM sample depth: %d\n", mFormat.wBitsPerSample);
  241. return false;
  242. }
  243. }
  244. else
  245. {
  246. ERR("Unhandled format tag: 0x%04x\n", mFormat.wFormatTag);
  247. return false;
  248. }
  249. uint chanmask{};
  250. if(mFormat.nChannels == 2)
  251. {
  252. mDevice->FmtChans = DevFmtStereo;
  253. chanmask = SPEAKER_FRONT_LEFT | SPEAKER_FRONT_RIGHT;
  254. }
  255. else if(mFormat.nChannels == 1)
  256. {
  257. mDevice->FmtChans = DevFmtMono;
  258. chanmask = SPEAKER_FRONT_CENTER;
  259. }
  260. else
  261. {
  262. ERR("Unhandled channel count: %d\n", mFormat.nChannels);
  263. return false;
  264. }
  265. setChannelOrderFromWFXMask(chanmask);
  266. uint BufferSize{mDevice->UpdateSize * mDevice->frameSizeFromFmt()};
  267. al_free(mWaveBuffer[0].lpData);
  268. mWaveBuffer[0] = WAVEHDR{};
  269. mWaveBuffer[0].lpData = static_cast<char*>(al_calloc(16, BufferSize * mWaveBuffer.size()));
  270. mWaveBuffer[0].dwBufferLength = BufferSize;
  271. for(size_t i{1};i < mWaveBuffer.size();i++)
  272. {
  273. mWaveBuffer[i] = WAVEHDR{};
  274. mWaveBuffer[i].lpData = mWaveBuffer[i-1].lpData + mWaveBuffer[i-1].dwBufferLength;
  275. mWaveBuffer[i].dwBufferLength = BufferSize;
  276. }
  277. mIdx = 0;
  278. return true;
  279. }
  280. void WinMMPlayback::start()
  281. {
  282. try {
  283. std::for_each(mWaveBuffer.begin(), mWaveBuffer.end(),
  284. [this](WAVEHDR &waveHdr) -> void
  285. { waveOutPrepareHeader(mOutHdl, &waveHdr, sizeof(WAVEHDR)); });
  286. mWritable.store(static_cast<uint>(mWaveBuffer.size()), std::memory_order_release);
  287. mKillNow.store(false, std::memory_order_release);
  288. mThread = std::thread{std::mem_fn(&WinMMPlayback::mixerProc), this};
  289. }
  290. catch(std::exception& e) {
  291. throw al::backend_exception{al::backend_error::DeviceError,
  292. "Failed to start mixing thread: %s", e.what()};
  293. }
  294. }
  295. void WinMMPlayback::stop()
  296. {
  297. if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
  298. return;
  299. mThread.join();
  300. while(mWritable.load(std::memory_order_acquire) < mWaveBuffer.size())
  301. mSem.wait();
  302. std::for_each(mWaveBuffer.begin(), mWaveBuffer.end(),
  303. [this](WAVEHDR &waveHdr) -> void
  304. { waveOutUnprepareHeader(mOutHdl, &waveHdr, sizeof(WAVEHDR)); });
  305. mWritable.store(0, std::memory_order_release);
  306. }
  307. struct WinMMCapture final : public BackendBase {
  308. WinMMCapture(ALCdevice *device) noexcept : BackendBase{device} { }
  309. ~WinMMCapture() override;
  310. void CALLBACK waveInProc(HWAVEIN device, UINT msg, DWORD_PTR param1, DWORD_PTR param2) noexcept;
  311. static void CALLBACK waveInProcC(HWAVEIN device, UINT msg, DWORD_PTR instance, DWORD_PTR param1, DWORD_PTR param2) noexcept
  312. { reinterpret_cast<WinMMCapture*>(instance)->waveInProc(device, msg, param1, param2); }
  313. int captureProc();
  314. void open(const char *name) override;
  315. void start() override;
  316. void stop() override;
  317. void captureSamples(al::byte *buffer, uint samples) override;
  318. uint availableSamples() override;
  319. std::atomic<uint> mReadable{0u};
  320. al::semaphore mSem;
  321. uint mIdx{0};
  322. std::array<WAVEHDR,4> mWaveBuffer{};
  323. HWAVEIN mInHdl{nullptr};
  324. RingBufferPtr mRing{nullptr};
  325. WAVEFORMATEX mFormat{};
  326. std::atomic<bool> mKillNow{true};
  327. std::thread mThread;
  328. DEF_NEWDEL(WinMMCapture)
  329. };
  330. WinMMCapture::~WinMMCapture()
  331. {
  332. // Close the Wave device
  333. if(mInHdl)
  334. waveInClose(mInHdl);
  335. mInHdl = nullptr;
  336. al_free(mWaveBuffer[0].lpData);
  337. std::fill(mWaveBuffer.begin(), mWaveBuffer.end(), WAVEHDR{});
  338. }
  339. /* WinMMCapture::waveInProc
  340. *
  341. * Posts a message to 'WinMMCapture::captureProc' everytime a WaveIn Buffer is
  342. * completed and returns to the application (with more data).
  343. */
  344. void CALLBACK WinMMCapture::waveInProc(HWAVEIN, UINT msg, DWORD_PTR, DWORD_PTR) noexcept
  345. {
  346. if(msg != WIM_DATA) return;
  347. mReadable.fetch_add(1, std::memory_order_acq_rel);
  348. mSem.post();
  349. }
  350. int WinMMCapture::captureProc()
  351. {
  352. althrd_setname(RECORD_THREAD_NAME);
  353. while(!mKillNow.load(std::memory_order_acquire) &&
  354. mDevice->Connected.load(std::memory_order_acquire))
  355. {
  356. uint todo{mReadable.load(std::memory_order_acquire)};
  357. if(todo < 1)
  358. {
  359. mSem.wait();
  360. continue;
  361. }
  362. size_t widx{mIdx};
  363. do {
  364. WAVEHDR &waveHdr = mWaveBuffer[widx];
  365. widx = (widx+1) % mWaveBuffer.size();
  366. mRing->write(waveHdr.lpData, waveHdr.dwBytesRecorded / mFormat.nBlockAlign);
  367. mReadable.fetch_sub(1, std::memory_order_acq_rel);
  368. waveInAddBuffer(mInHdl, &waveHdr, sizeof(WAVEHDR));
  369. } while(--todo);
  370. mIdx = static_cast<uint>(widx);
  371. }
  372. return 0;
  373. }
  374. void WinMMCapture::open(const char *name)
  375. {
  376. if(CaptureDevices.empty())
  377. ProbeCaptureDevices();
  378. // Find the Device ID matching the deviceName if valid
  379. auto iter = name ?
  380. std::find(CaptureDevices.cbegin(), CaptureDevices.cend(), name) :
  381. CaptureDevices.cbegin();
  382. if(iter == CaptureDevices.cend())
  383. throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%s\" not found",
  384. name};
  385. auto DeviceID = static_cast<UINT>(std::distance(CaptureDevices.cbegin(), iter));
  386. switch(mDevice->FmtChans)
  387. {
  388. case DevFmtMono:
  389. case DevFmtStereo:
  390. break;
  391. case DevFmtQuad:
  392. case DevFmtX51:
  393. case DevFmtX51Rear:
  394. case DevFmtX61:
  395. case DevFmtX71:
  396. case DevFmtAmbi3D:
  397. throw al::backend_exception{al::backend_error::DeviceError, "%s capture not supported",
  398. DevFmtChannelsString(mDevice->FmtChans)};
  399. }
  400. switch(mDevice->FmtType)
  401. {
  402. case DevFmtUByte:
  403. case DevFmtShort:
  404. case DevFmtInt:
  405. case DevFmtFloat:
  406. break;
  407. case DevFmtByte:
  408. case DevFmtUShort:
  409. case DevFmtUInt:
  410. throw al::backend_exception{al::backend_error::DeviceError, "%s samples not supported",
  411. DevFmtTypeString(mDevice->FmtType)};
  412. }
  413. mFormat = WAVEFORMATEX{};
  414. mFormat.wFormatTag = (mDevice->FmtType == DevFmtFloat) ?
  415. WAVE_FORMAT_IEEE_FLOAT : WAVE_FORMAT_PCM;
  416. mFormat.nChannels = static_cast<WORD>(mDevice->channelsFromFmt());
  417. mFormat.wBitsPerSample = static_cast<WORD>(mDevice->bytesFromFmt() * 8);
  418. mFormat.nBlockAlign = static_cast<WORD>(mFormat.wBitsPerSample * mFormat.nChannels / 8);
  419. mFormat.nSamplesPerSec = mDevice->Frequency;
  420. mFormat.nAvgBytesPerSec = mFormat.nSamplesPerSec * mFormat.nBlockAlign;
  421. mFormat.cbSize = 0;
  422. MMRESULT res{waveInOpen(&mInHdl, DeviceID, &mFormat,
  423. reinterpret_cast<DWORD_PTR>(&WinMMCapture::waveInProcC),
  424. reinterpret_cast<DWORD_PTR>(this), CALLBACK_FUNCTION)};
  425. if(res != MMSYSERR_NOERROR)
  426. throw al::backend_exception{al::backend_error::DeviceError, "waveInOpen failed: %u", res};
  427. // Ensure each buffer is 50ms each
  428. DWORD BufferSize{mFormat.nAvgBytesPerSec / 20u};
  429. BufferSize -= (BufferSize % mFormat.nBlockAlign);
  430. // Allocate circular memory buffer for the captured audio
  431. // Make sure circular buffer is at least 100ms in size
  432. uint CapturedDataSize{mDevice->BufferSize};
  433. CapturedDataSize = static_cast<uint>(maxz(CapturedDataSize, BufferSize*mWaveBuffer.size()));
  434. mRing = RingBuffer::Create(CapturedDataSize, mFormat.nBlockAlign, false);
  435. al_free(mWaveBuffer[0].lpData);
  436. mWaveBuffer[0] = WAVEHDR{};
  437. mWaveBuffer[0].lpData = static_cast<char*>(al_calloc(16, BufferSize * mWaveBuffer.size()));
  438. mWaveBuffer[0].dwBufferLength = BufferSize;
  439. for(size_t i{1};i < mWaveBuffer.size();++i)
  440. {
  441. mWaveBuffer[i] = WAVEHDR{};
  442. mWaveBuffer[i].lpData = mWaveBuffer[i-1].lpData + mWaveBuffer[i-1].dwBufferLength;
  443. mWaveBuffer[i].dwBufferLength = mWaveBuffer[i-1].dwBufferLength;
  444. }
  445. mDevice->DeviceName = CaptureDevices[DeviceID];
  446. }
  447. void WinMMCapture::start()
  448. {
  449. try {
  450. for(size_t i{0};i < mWaveBuffer.size();++i)
  451. {
  452. waveInPrepareHeader(mInHdl, &mWaveBuffer[i], sizeof(WAVEHDR));
  453. waveInAddBuffer(mInHdl, &mWaveBuffer[i], sizeof(WAVEHDR));
  454. }
  455. mKillNow.store(false, std::memory_order_release);
  456. mThread = std::thread{std::mem_fn(&WinMMCapture::captureProc), this};
  457. waveInStart(mInHdl);
  458. }
  459. catch(std::exception& e) {
  460. throw al::backend_exception{al::backend_error::DeviceError,
  461. "Failed to start recording thread: %s", e.what()};
  462. }
  463. }
  464. void WinMMCapture::stop()
  465. {
  466. waveInStop(mInHdl);
  467. mKillNow.store(true, std::memory_order_release);
  468. if(mThread.joinable())
  469. {
  470. mSem.post();
  471. mThread.join();
  472. }
  473. waveInReset(mInHdl);
  474. for(size_t i{0};i < mWaveBuffer.size();++i)
  475. waveInUnprepareHeader(mInHdl, &mWaveBuffer[i], sizeof(WAVEHDR));
  476. mReadable.store(0, std::memory_order_release);
  477. mIdx = 0;
  478. }
  479. void WinMMCapture::captureSamples(al::byte *buffer, uint samples)
  480. { mRing->read(buffer, samples); }
  481. uint WinMMCapture::availableSamples()
  482. { return static_cast<uint>(mRing->readSpace()); }
  483. } // namespace
  484. bool WinMMBackendFactory::init()
  485. { return true; }
  486. bool WinMMBackendFactory::querySupport(BackendType type)
  487. { return type == BackendType::Playback || type == BackendType::Capture; }
  488. std::string WinMMBackendFactory::probe(BackendType type)
  489. {
  490. std::string outnames;
  491. auto add_device = [&outnames](const std::string &dname) -> void
  492. {
  493. /* +1 to also append the null char (to ensure a null-separated list and
  494. * double-null terminated list).
  495. */
  496. if(!dname.empty())
  497. outnames.append(dname.c_str(), dname.length()+1);
  498. };
  499. switch(type)
  500. {
  501. case BackendType::Playback:
  502. ProbePlaybackDevices();
  503. std::for_each(PlaybackDevices.cbegin(), PlaybackDevices.cend(), add_device);
  504. break;
  505. case BackendType::Capture:
  506. ProbeCaptureDevices();
  507. std::for_each(CaptureDevices.cbegin(), CaptureDevices.cend(), add_device);
  508. break;
  509. }
  510. return outnames;
  511. }
  512. BackendPtr WinMMBackendFactory::createBackend(ALCdevice *device, BackendType type)
  513. {
  514. if(type == BackendType::Playback)
  515. return BackendPtr{new WinMMPlayback{device}};
  516. if(type == BackendType::Capture)
  517. return BackendPtr{new WinMMCapture{device}};
  518. return nullptr;
  519. }
  520. BackendFactory &WinMMBackendFactory::getFactory()
  521. {
  522. static WinMMBackendFactory factory{};
  523. return factory;
  524. }