oss.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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/oss.h"
  22. #include <fcntl.h>
  23. #include <poll.h>
  24. #include <sys/ioctl.h>
  25. #include <sys/stat.h>
  26. #include <unistd.h>
  27. #include <algorithm>
  28. #include <atomic>
  29. #include <cerrno>
  30. #include <cstdio>
  31. #include <cstring>
  32. #include <exception>
  33. #include <functional>
  34. #include <memory>
  35. #include <new>
  36. #include <string>
  37. #include <thread>
  38. #include <utility>
  39. #include "alcmain.h"
  40. #include "alconfig.h"
  41. #include "albyte.h"
  42. #include "almalloc.h"
  43. #include "alnumeric.h"
  44. #include "aloptional.h"
  45. #include "alu.h"
  46. #include "core/logging.h"
  47. #include "ringbuffer.h"
  48. #include "threads.h"
  49. #include "vector.h"
  50. #include <sys/soundcard.h>
  51. /*
  52. * The OSS documentation talks about SOUND_MIXER_READ, but the header
  53. * only contains MIXER_READ. Play safe. Same for WRITE.
  54. */
  55. #ifndef SOUND_MIXER_READ
  56. #define SOUND_MIXER_READ MIXER_READ
  57. #endif
  58. #ifndef SOUND_MIXER_WRITE
  59. #define SOUND_MIXER_WRITE MIXER_WRITE
  60. #endif
  61. #if defined(SOUND_VERSION) && (SOUND_VERSION < 0x040000)
  62. #define ALC_OSS_COMPAT
  63. #endif
  64. #ifndef SNDCTL_AUDIOINFO
  65. #define ALC_OSS_COMPAT
  66. #endif
  67. /*
  68. * FreeBSD strongly discourages the use of specific devices,
  69. * such as those returned in oss_audioinfo.devnode
  70. */
  71. #ifdef __FreeBSD__
  72. #define ALC_OSS_DEVNODE_TRUC
  73. #endif
  74. namespace {
  75. constexpr char DefaultName[] = "OSS Default";
  76. std::string DefaultPlayback{"/dev/dsp"};
  77. std::string DefaultCapture{"/dev/dsp"};
  78. struct DevMap {
  79. std::string name;
  80. std::string device_name;
  81. };
  82. al::vector<DevMap> PlaybackDevices;
  83. al::vector<DevMap> CaptureDevices;
  84. #ifdef ALC_OSS_COMPAT
  85. #define DSP_CAP_OUTPUT 0x00020000
  86. #define DSP_CAP_INPUT 0x00010000
  87. void ALCossListPopulate(al::vector<DevMap> &devlist, int type)
  88. {
  89. devlist.emplace_back(DevMap{DefaultName, (type==DSP_CAP_INPUT) ? DefaultCapture : DefaultPlayback});
  90. }
  91. #else
  92. void ALCossListAppend(al::vector<DevMap> &list, al::span<const char> handle, al::span<const char> path)
  93. {
  94. #ifdef ALC_OSS_DEVNODE_TRUC
  95. for(size_t i{0};i < path.size();++i)
  96. {
  97. if(path[i] == '.' && handle.size() + i >= path.size())
  98. {
  99. const size_t hoffset{handle.size() + i - path.size()};
  100. if(strncmp(path.data() + i, handle.data() + hoffset, path.size() - i) == 0)
  101. handle = handle.first(hoffset);
  102. path = path.first(i);
  103. }
  104. }
  105. #endif
  106. if(handle.empty())
  107. handle = path;
  108. std::string basename{handle.data(), handle.size()};
  109. std::string devname{path.data(), path.size()};
  110. auto match_devname = [&devname](const DevMap &entry) -> bool
  111. { return entry.device_name == devname; };
  112. if(std::find_if(list.cbegin(), list.cend(), match_devname) != list.cend())
  113. return;
  114. auto checkName = [&list](const std::string &name) -> bool
  115. {
  116. auto match_name = [&name](const DevMap &entry) -> bool { return entry.name == name; };
  117. return std::find_if(list.cbegin(), list.cend(), match_name) != list.cend();
  118. };
  119. int count{1};
  120. std::string newname{basename};
  121. while(checkName(newname))
  122. {
  123. newname = basename;
  124. newname += " #";
  125. newname += std::to_string(++count);
  126. }
  127. list.emplace_back(DevMap{std::move(newname), std::move(devname)});
  128. const DevMap &entry = list.back();
  129. TRACE("Got device \"%s\", \"%s\"\n", entry.name.c_str(), entry.device_name.c_str());
  130. }
  131. void ALCossListPopulate(al::vector<DevMap> &devlist, int type_flag)
  132. {
  133. int fd{open("/dev/mixer", O_RDONLY)};
  134. if(fd < 0)
  135. {
  136. TRACE("Could not open /dev/mixer: %s\n", strerror(errno));
  137. goto done;
  138. }
  139. oss_sysinfo si;
  140. if(ioctl(fd, SNDCTL_SYSINFO, &si) == -1)
  141. {
  142. TRACE("SNDCTL_SYSINFO failed: %s\n", strerror(errno));
  143. goto done;
  144. }
  145. for(int i{0};i < si.numaudios;i++)
  146. {
  147. oss_audioinfo ai;
  148. ai.dev = i;
  149. if(ioctl(fd, SNDCTL_AUDIOINFO, &ai) == -1)
  150. {
  151. ERR("SNDCTL_AUDIOINFO (%d) failed: %s\n", i, strerror(errno));
  152. continue;
  153. }
  154. if(!(ai.caps&type_flag) || ai.devnode[0] == '\0')
  155. continue;
  156. al::span<const char> handle;
  157. if(ai.handle[0] != '\0')
  158. handle = {ai.handle, strnlen(ai.handle, sizeof(ai.handle))};
  159. else
  160. handle = {ai.name, strnlen(ai.name, sizeof(ai.name))};
  161. al::span<const char> devnode{ai.devnode, strnlen(ai.devnode, sizeof(ai.devnode))};
  162. ALCossListAppend(devlist, handle, devnode);
  163. }
  164. done:
  165. if(fd >= 0)
  166. close(fd);
  167. fd = -1;
  168. const char *defdev{((type_flag==DSP_CAP_INPUT) ? DefaultCapture : DefaultPlayback).c_str()};
  169. auto iter = std::find_if(devlist.cbegin(), devlist.cend(),
  170. [defdev](const DevMap &entry) -> bool
  171. { return entry.device_name == defdev; }
  172. );
  173. if(iter == devlist.cend())
  174. devlist.insert(devlist.begin(), DevMap{DefaultName, defdev});
  175. else
  176. {
  177. DevMap entry{std::move(*iter)};
  178. devlist.erase(iter);
  179. devlist.insert(devlist.begin(), std::move(entry));
  180. }
  181. devlist.shrink_to_fit();
  182. }
  183. #endif
  184. uint log2i(uint x)
  185. {
  186. uint y{0};
  187. while(x > 1)
  188. {
  189. x >>= 1;
  190. y++;
  191. }
  192. return y;
  193. }
  194. struct OSSPlayback final : public BackendBase {
  195. OSSPlayback(ALCdevice *device) noexcept : BackendBase{device} { }
  196. ~OSSPlayback() override;
  197. int mixerProc();
  198. void open(const char *name) override;
  199. bool reset() override;
  200. void start() override;
  201. void stop() override;
  202. int mFd{-1};
  203. al::vector<al::byte> mMixData;
  204. std::atomic<bool> mKillNow{true};
  205. std::thread mThread;
  206. DEF_NEWDEL(OSSPlayback)
  207. };
  208. OSSPlayback::~OSSPlayback()
  209. {
  210. if(mFd != -1)
  211. close(mFd);
  212. mFd = -1;
  213. }
  214. int OSSPlayback::mixerProc()
  215. {
  216. SetRTPriority();
  217. althrd_setname(MIXER_THREAD_NAME);
  218. const size_t frame_step{mDevice->channelsFromFmt()};
  219. const size_t frame_size{mDevice->frameSizeFromFmt()};
  220. while(!mKillNow.load(std::memory_order_acquire)
  221. && mDevice->Connected.load(std::memory_order_acquire))
  222. {
  223. pollfd pollitem{};
  224. pollitem.fd = mFd;
  225. pollitem.events = POLLOUT;
  226. int pret{poll(&pollitem, 1, 1000)};
  227. if(pret < 0)
  228. {
  229. if(errno == EINTR || errno == EAGAIN)
  230. continue;
  231. ERR("poll failed: %s\n", strerror(errno));
  232. mDevice->handleDisconnect("Failed waiting for playback buffer: %s", strerror(errno));
  233. break;
  234. }
  235. else if(pret == 0)
  236. {
  237. WARN("poll timeout\n");
  238. continue;
  239. }
  240. al::byte *write_ptr{mMixData.data()};
  241. size_t to_write{mMixData.size()};
  242. mDevice->renderSamples(write_ptr, static_cast<uint>(to_write/frame_size), frame_step);
  243. while(to_write > 0 && !mKillNow.load(std::memory_order_acquire))
  244. {
  245. ssize_t wrote{write(mFd, write_ptr, to_write)};
  246. if(wrote < 0)
  247. {
  248. if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
  249. continue;
  250. ERR("write failed: %s\n", strerror(errno));
  251. mDevice->handleDisconnect("Failed writing playback samples: %s", strerror(errno));
  252. break;
  253. }
  254. to_write -= static_cast<size_t>(wrote);
  255. write_ptr += wrote;
  256. }
  257. }
  258. return 0;
  259. }
  260. void OSSPlayback::open(const char *name)
  261. {
  262. const char *devname{DefaultPlayback.c_str()};
  263. if(!name)
  264. name = DefaultName;
  265. else
  266. {
  267. if(PlaybackDevices.empty())
  268. ALCossListPopulate(PlaybackDevices, DSP_CAP_OUTPUT);
  269. auto iter = std::find_if(PlaybackDevices.cbegin(), PlaybackDevices.cend(),
  270. [&name](const DevMap &entry) -> bool
  271. { return entry.name == name; }
  272. );
  273. if(iter == PlaybackDevices.cend())
  274. throw al::backend_exception{al::backend_error::NoDevice,
  275. "Device name \"%s\" not found", name};
  276. devname = iter->device_name.c_str();
  277. }
  278. mFd = ::open(devname, O_WRONLY);
  279. if(mFd == -1)
  280. throw al::backend_exception{al::backend_error::NoDevice, "Could not open %s: %s", devname,
  281. strerror(errno)};
  282. mDevice->DeviceName = name;
  283. }
  284. bool OSSPlayback::reset()
  285. {
  286. int ossFormat{};
  287. switch(mDevice->FmtType)
  288. {
  289. case DevFmtByte:
  290. ossFormat = AFMT_S8;
  291. break;
  292. case DevFmtUByte:
  293. ossFormat = AFMT_U8;
  294. break;
  295. case DevFmtUShort:
  296. case DevFmtInt:
  297. case DevFmtUInt:
  298. case DevFmtFloat:
  299. mDevice->FmtType = DevFmtShort;
  300. /* fall-through */
  301. case DevFmtShort:
  302. ossFormat = AFMT_S16_NE;
  303. break;
  304. }
  305. uint periods{mDevice->BufferSize / mDevice->UpdateSize};
  306. uint numChannels{mDevice->channelsFromFmt()};
  307. uint ossSpeed{mDevice->Frequency};
  308. uint frameSize{numChannels * mDevice->bytesFromFmt()};
  309. /* According to the OSS spec, 16 bytes (log2(16)) is the minimum. */
  310. uint log2FragmentSize{maxu(log2i(mDevice->UpdateSize*frameSize), 4)};
  311. uint numFragmentsLogSize{(periods << 16) | log2FragmentSize};
  312. audio_buf_info info{};
  313. const char *err;
  314. #define CHECKERR(func) if((func) < 0) { \
  315. err = #func; \
  316. goto err; \
  317. }
  318. /* Don't fail if SETFRAGMENT fails. We can handle just about anything
  319. * that's reported back via GETOSPACE */
  320. ioctl(mFd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize);
  321. CHECKERR(ioctl(mFd, SNDCTL_DSP_SETFMT, &ossFormat));
  322. CHECKERR(ioctl(mFd, SNDCTL_DSP_CHANNELS, &numChannels));
  323. CHECKERR(ioctl(mFd, SNDCTL_DSP_SPEED, &ossSpeed));
  324. CHECKERR(ioctl(mFd, SNDCTL_DSP_GETOSPACE, &info));
  325. if(0)
  326. {
  327. err:
  328. ERR("%s failed: %s\n", err, strerror(errno));
  329. return false;
  330. }
  331. #undef CHECKERR
  332. if(mDevice->channelsFromFmt() != numChannels)
  333. {
  334. ERR("Failed to set %s, got %d channels instead\n", DevFmtChannelsString(mDevice->FmtChans),
  335. numChannels);
  336. return false;
  337. }
  338. if(!((ossFormat == AFMT_S8 && mDevice->FmtType == DevFmtByte) ||
  339. (ossFormat == AFMT_U8 && mDevice->FmtType == DevFmtUByte) ||
  340. (ossFormat == AFMT_S16_NE && mDevice->FmtType == DevFmtShort)))
  341. {
  342. ERR("Failed to set %s samples, got OSS format %#x\n", DevFmtTypeString(mDevice->FmtType),
  343. ossFormat);
  344. return false;
  345. }
  346. mDevice->Frequency = ossSpeed;
  347. mDevice->UpdateSize = static_cast<uint>(info.fragsize) / frameSize;
  348. mDevice->BufferSize = static_cast<uint>(info.fragments) * mDevice->UpdateSize;
  349. setDefaultChannelOrder();
  350. mMixData.resize(mDevice->UpdateSize * mDevice->frameSizeFromFmt());
  351. return true;
  352. }
  353. void OSSPlayback::start()
  354. {
  355. try {
  356. mKillNow.store(false, std::memory_order_release);
  357. mThread = std::thread{std::mem_fn(&OSSPlayback::mixerProc), this};
  358. }
  359. catch(std::exception& e) {
  360. throw al::backend_exception{al::backend_error::DeviceError,
  361. "Failed to start mixing thread: %s", e.what()};
  362. }
  363. }
  364. void OSSPlayback::stop()
  365. {
  366. if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
  367. return;
  368. mThread.join();
  369. if(ioctl(mFd, SNDCTL_DSP_RESET) != 0)
  370. ERR("Error resetting device: %s\n", strerror(errno));
  371. }
  372. struct OSScapture final : public BackendBase {
  373. OSScapture(ALCdevice *device) noexcept : BackendBase{device} { }
  374. ~OSScapture() override;
  375. int recordProc();
  376. void open(const char *name) override;
  377. void start() override;
  378. void stop() override;
  379. void captureSamples(al::byte *buffer, uint samples) override;
  380. uint availableSamples() override;
  381. int mFd{-1};
  382. RingBufferPtr mRing{nullptr};
  383. std::atomic<bool> mKillNow{true};
  384. std::thread mThread;
  385. DEF_NEWDEL(OSScapture)
  386. };
  387. OSScapture::~OSScapture()
  388. {
  389. if(mFd != -1)
  390. close(mFd);
  391. mFd = -1;
  392. }
  393. int OSScapture::recordProc()
  394. {
  395. SetRTPriority();
  396. althrd_setname(RECORD_THREAD_NAME);
  397. const size_t frame_size{mDevice->frameSizeFromFmt()};
  398. while(!mKillNow.load(std::memory_order_acquire))
  399. {
  400. pollfd pollitem{};
  401. pollitem.fd = mFd;
  402. pollitem.events = POLLIN;
  403. int sret{poll(&pollitem, 1, 1000)};
  404. if(sret < 0)
  405. {
  406. if(errno == EINTR || errno == EAGAIN)
  407. continue;
  408. ERR("poll failed: %s\n", strerror(errno));
  409. mDevice->handleDisconnect("Failed to check capture samples: %s", strerror(errno));
  410. break;
  411. }
  412. else if(sret == 0)
  413. {
  414. WARN("poll timeout\n");
  415. continue;
  416. }
  417. auto vec = mRing->getWriteVector();
  418. if(vec.first.len > 0)
  419. {
  420. ssize_t amt{read(mFd, vec.first.buf, vec.first.len*frame_size)};
  421. if(amt < 0)
  422. {
  423. ERR("read failed: %s\n", strerror(errno));
  424. mDevice->handleDisconnect("Failed reading capture samples: %s", strerror(errno));
  425. break;
  426. }
  427. mRing->writeAdvance(static_cast<size_t>(amt)/frame_size);
  428. }
  429. }
  430. return 0;
  431. }
  432. void OSScapture::open(const char *name)
  433. {
  434. const char *devname{DefaultCapture.c_str()};
  435. if(!name)
  436. name = DefaultName;
  437. else
  438. {
  439. if(CaptureDevices.empty())
  440. ALCossListPopulate(CaptureDevices, DSP_CAP_INPUT);
  441. auto iter = std::find_if(CaptureDevices.cbegin(), CaptureDevices.cend(),
  442. [&name](const DevMap &entry) -> bool
  443. { return entry.name == name; }
  444. );
  445. if(iter == CaptureDevices.cend())
  446. throw al::backend_exception{al::backend_error::NoDevice,
  447. "Device name \"%s\" not found", name};
  448. devname = iter->device_name.c_str();
  449. }
  450. mFd = ::open(devname, O_RDONLY);
  451. if(mFd == -1)
  452. throw al::backend_exception{al::backend_error::NoDevice, "Could not open %s: %s", devname,
  453. strerror(errno)};
  454. int ossFormat{};
  455. switch(mDevice->FmtType)
  456. {
  457. case DevFmtByte:
  458. ossFormat = AFMT_S8;
  459. break;
  460. case DevFmtUByte:
  461. ossFormat = AFMT_U8;
  462. break;
  463. case DevFmtShort:
  464. ossFormat = AFMT_S16_NE;
  465. break;
  466. case DevFmtUShort:
  467. case DevFmtInt:
  468. case DevFmtUInt:
  469. case DevFmtFloat:
  470. throw al::backend_exception{al::backend_error::DeviceError,
  471. "%s capture samples not supported", DevFmtTypeString(mDevice->FmtType)};
  472. }
  473. uint periods{4};
  474. uint numChannels{mDevice->channelsFromFmt()};
  475. uint frameSize{numChannels * mDevice->bytesFromFmt()};
  476. uint ossSpeed{mDevice->Frequency};
  477. /* according to the OSS spec, 16 bytes are the minimum */
  478. uint log2FragmentSize{maxu(log2i(mDevice->BufferSize * frameSize / periods), 4)};
  479. uint numFragmentsLogSize{(periods << 16) | log2FragmentSize};
  480. audio_buf_info info{};
  481. #define CHECKERR(func) if((func) < 0) { \
  482. throw al::backend_exception{al::backend_error::DeviceError, #func " failed: %s", \
  483. strerror(errno)}; \
  484. }
  485. CHECKERR(ioctl(mFd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize));
  486. CHECKERR(ioctl(mFd, SNDCTL_DSP_SETFMT, &ossFormat));
  487. CHECKERR(ioctl(mFd, SNDCTL_DSP_CHANNELS, &numChannels));
  488. CHECKERR(ioctl(mFd, SNDCTL_DSP_SPEED, &ossSpeed));
  489. CHECKERR(ioctl(mFd, SNDCTL_DSP_GETISPACE, &info));
  490. #undef CHECKERR
  491. if(mDevice->channelsFromFmt() != numChannels)
  492. throw al::backend_exception{al::backend_error::DeviceError,
  493. "Failed to set %s, got %d channels instead", DevFmtChannelsString(mDevice->FmtChans),
  494. numChannels};
  495. if(!((ossFormat == AFMT_S8 && mDevice->FmtType == DevFmtByte)
  496. || (ossFormat == AFMT_U8 && mDevice->FmtType == DevFmtUByte)
  497. || (ossFormat == AFMT_S16_NE && mDevice->FmtType == DevFmtShort)))
  498. throw al::backend_exception{al::backend_error::DeviceError,
  499. "Failed to set %s samples, got OSS format %#x", DevFmtTypeString(mDevice->FmtType),
  500. ossFormat};
  501. mRing = RingBuffer::Create(mDevice->BufferSize, frameSize, false);
  502. mDevice->DeviceName = name;
  503. }
  504. void OSScapture::start()
  505. {
  506. try {
  507. mKillNow.store(false, std::memory_order_release);
  508. mThread = std::thread{std::mem_fn(&OSScapture::recordProc), this};
  509. }
  510. catch(std::exception& e) {
  511. throw al::backend_exception{al::backend_error::DeviceError,
  512. "Failed to start recording thread: %s", e.what()};
  513. }
  514. }
  515. void OSScapture::stop()
  516. {
  517. if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
  518. return;
  519. mThread.join();
  520. if(ioctl(mFd, SNDCTL_DSP_RESET) != 0)
  521. ERR("Error resetting device: %s\n", strerror(errno));
  522. }
  523. void OSScapture::captureSamples(al::byte *buffer, uint samples)
  524. { mRing->read(buffer, samples); }
  525. uint OSScapture::availableSamples()
  526. { return static_cast<uint>(mRing->readSpace()); }
  527. } // namespace
  528. BackendFactory &OSSBackendFactory::getFactory()
  529. {
  530. static OSSBackendFactory factory{};
  531. return factory;
  532. }
  533. bool OSSBackendFactory::init()
  534. {
  535. if(auto devopt = ConfigValueStr(nullptr, "oss", "device"))
  536. DefaultPlayback = std::move(*devopt);
  537. if(auto capopt = ConfigValueStr(nullptr, "oss", "capture"))
  538. DefaultCapture = std::move(*capopt);
  539. return true;
  540. }
  541. bool OSSBackendFactory::querySupport(BackendType type)
  542. { return (type == BackendType::Playback || type == BackendType::Capture); }
  543. std::string OSSBackendFactory::probe(BackendType type)
  544. {
  545. std::string outnames;
  546. auto add_device = [&outnames](const DevMap &entry) -> void
  547. {
  548. struct stat buf;
  549. if(stat(entry.device_name.c_str(), &buf) == 0)
  550. {
  551. /* Includes null char. */
  552. outnames.append(entry.name.c_str(), entry.name.length()+1);
  553. }
  554. };
  555. switch(type)
  556. {
  557. case BackendType::Playback:
  558. PlaybackDevices.clear();
  559. ALCossListPopulate(PlaybackDevices, DSP_CAP_OUTPUT);
  560. std::for_each(PlaybackDevices.cbegin(), PlaybackDevices.cend(), add_device);
  561. break;
  562. case BackendType::Capture:
  563. CaptureDevices.clear();
  564. ALCossListPopulate(CaptureDevices, DSP_CAP_INPUT);
  565. std::for_each(CaptureDevices.cbegin(), CaptureDevices.cend(), add_device);
  566. break;
  567. }
  568. return outnames;
  569. }
  570. BackendPtr OSSBackendFactory::createBackend(ALCdevice *device, BackendType type)
  571. {
  572. if(type == BackendType::Playback)
  573. return BackendPtr{new OSSPlayback{device}};
  574. if(type == BackendType::Capture)
  575. return BackendPtr{new OSScapture{device}};
  576. return nullptr;
  577. }