oss.cpp 19 KB

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