solaris.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 "solaris.h"
  22. #include <sys/ioctl.h>
  23. #include <sys/types.h>
  24. #include <sys/time.h>
  25. #include <sys/stat.h>
  26. #include <fcntl.h>
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <memory.h>
  30. #include <unistd.h>
  31. #include <errno.h>
  32. #include <poll.h>
  33. #include <math.h>
  34. #include <string.h>
  35. #include <vector>
  36. #include <thread>
  37. #include <functional>
  38. #include "alc/alconfig.h"
  39. #include "alstring.h"
  40. #include "althrd_setname.h"
  41. #include "core/device.h"
  42. #include "core/helpers.h"
  43. #include "core/logging.h"
  44. #include <sys/audioio.h>
  45. namespace {
  46. using namespace std::string_view_literals;
  47. [[nodiscard]] constexpr auto GetDefaultName() noexcept { return "Solaris Default"sv; }
  48. std::string solaris_driver{"/dev/audio"};
  49. struct SolarisBackend final : public BackendBase {
  50. SolarisBackend(DeviceBase *device) noexcept : BackendBase{device} { }
  51. ~SolarisBackend() override;
  52. int mixerProc();
  53. void open(std::string_view name) override;
  54. bool reset() override;
  55. void start() override;
  56. void stop() override;
  57. int mFd{-1};
  58. uint mFrameStep{};
  59. std::vector<std::byte> mBuffer;
  60. std::atomic<bool> mKillNow{true};
  61. std::thread mThread;
  62. };
  63. SolarisBackend::~SolarisBackend()
  64. {
  65. if(mFd != -1)
  66. close(mFd);
  67. mFd = -1;
  68. }
  69. int SolarisBackend::mixerProc()
  70. {
  71. SetRTPriority();
  72. althrd_setname(GetMixerThreadName());
  73. const size_t frame_step{mDevice->channelsFromFmt()};
  74. const size_t frame_size{mDevice->frameSizeFromFmt()};
  75. while(!mKillNow.load(std::memory_order_acquire)
  76. && mDevice->Connected.load(std::memory_order_acquire))
  77. {
  78. pollfd pollitem{};
  79. pollitem.fd = mFd;
  80. pollitem.events = POLLOUT;
  81. int pret{poll(&pollitem, 1, 1000)};
  82. if(pret < 0)
  83. {
  84. if(errno == EINTR || errno == EAGAIN)
  85. continue;
  86. ERR("poll failed: %s\n", strerror(errno));
  87. mDevice->handleDisconnect("Failed to wait for playback buffer: %s", strerror(errno));
  88. break;
  89. }
  90. else if(pret == 0)
  91. {
  92. WARN("poll timeout\n");
  93. continue;
  94. }
  95. al::span<std::byte> buffer{mBuffer};
  96. mDevice->renderSamples(buffer.data(), static_cast<uint>(buffer.size()/frame_size),
  97. frame_step);
  98. while(!buffer.empty() && !mKillNow.load(std::memory_order_acquire))
  99. {
  100. ssize_t wrote{write(mFd, buffer.data(), buffer.size())};
  101. if(wrote < 0)
  102. {
  103. if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
  104. continue;
  105. ERR("write failed: %s\n", strerror(errno));
  106. mDevice->handleDisconnect("Failed to write playback samples: %s", strerror(errno));
  107. break;
  108. }
  109. buffer = buffer.subspan(static_cast<size_t>(wrote));
  110. }
  111. }
  112. return 0;
  113. }
  114. void SolarisBackend::open(std::string_view name)
  115. {
  116. if(name.empty())
  117. name = GetDefaultName();
  118. else if(name != GetDefaultName())
  119. throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%.*s\" not found",
  120. al::sizei(name), name.data()};
  121. int fd{::open(solaris_driver.c_str(), O_WRONLY)};
  122. if(fd == -1)
  123. throw al::backend_exception{al::backend_error::NoDevice, "Could not open %s: %s",
  124. solaris_driver.c_str(), strerror(errno)};
  125. if(mFd != -1)
  126. ::close(mFd);
  127. mFd = fd;
  128. mDevice->DeviceName = name;
  129. }
  130. bool SolarisBackend::reset()
  131. {
  132. audio_info_t info;
  133. AUDIO_INITINFO(&info);
  134. info.play.sample_rate = mDevice->Frequency;
  135. info.play.channels = mDevice->channelsFromFmt();
  136. switch(mDevice->FmtType)
  137. {
  138. case DevFmtByte:
  139. info.play.precision = 8;
  140. info.play.encoding = AUDIO_ENCODING_LINEAR;
  141. break;
  142. case DevFmtUByte:
  143. info.play.precision = 8;
  144. info.play.encoding = AUDIO_ENCODING_LINEAR8;
  145. break;
  146. case DevFmtUShort:
  147. case DevFmtInt:
  148. case DevFmtUInt:
  149. case DevFmtFloat:
  150. mDevice->FmtType = DevFmtShort;
  151. /* fall-through */
  152. case DevFmtShort:
  153. info.play.precision = 16;
  154. info.play.encoding = AUDIO_ENCODING_LINEAR;
  155. break;
  156. }
  157. info.play.buffer_size = mDevice->BufferSize * mDevice->frameSizeFromFmt();
  158. if(ioctl(mFd, AUDIO_SETINFO, &info) < 0)
  159. {
  160. ERR("ioctl failed: %s\n", strerror(errno));
  161. return false;
  162. }
  163. if(mDevice->channelsFromFmt() != info.play.channels)
  164. {
  165. if(info.play.channels >= 2)
  166. mDevice->FmtChans = DevFmtStereo;
  167. else if(info.play.channels == 1)
  168. mDevice->FmtChans = DevFmtMono;
  169. else
  170. throw al::backend_exception{al::backend_error::DeviceError,
  171. "Got %u device channels", info.play.channels};
  172. }
  173. if(info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR8)
  174. mDevice->FmtType = DevFmtUByte;
  175. else if(info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR)
  176. mDevice->FmtType = DevFmtByte;
  177. else if(info.play.precision == 16 && info.play.encoding == AUDIO_ENCODING_LINEAR)
  178. mDevice->FmtType = DevFmtShort;
  179. else if(info.play.precision == 32 && info.play.encoding == AUDIO_ENCODING_LINEAR)
  180. mDevice->FmtType = DevFmtInt;
  181. else
  182. {
  183. ERR("Got unhandled sample type: %d (0x%x)\n", info.play.precision, info.play.encoding);
  184. return false;
  185. }
  186. uint frame_size{mDevice->bytesFromFmt() * info.play.channels};
  187. mFrameStep = info.play.channels;
  188. mDevice->Frequency = info.play.sample_rate;
  189. mDevice->BufferSize = info.play.buffer_size / frame_size;
  190. /* How to get the actual period size/count? */
  191. mDevice->UpdateSize = mDevice->BufferSize / 2;
  192. setDefaultChannelOrder();
  193. mBuffer.resize(mDevice->UpdateSize * size_t{frame_size});
  194. std::fill(mBuffer.begin(), mBuffer.end(), std::byte{});
  195. return true;
  196. }
  197. void SolarisBackend::start()
  198. {
  199. try {
  200. mKillNow.store(false, std::memory_order_release);
  201. mThread = std::thread{std::mem_fn(&SolarisBackend::mixerProc), this};
  202. }
  203. catch(std::exception& e) {
  204. throw al::backend_exception{al::backend_error::DeviceError,
  205. "Failed to start mixing thread: %s", e.what()};
  206. }
  207. }
  208. void SolarisBackend::stop()
  209. {
  210. if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
  211. return;
  212. mThread.join();
  213. if(ioctl(mFd, AUDIO_DRAIN) < 0)
  214. ERR("Error draining device: %s\n", strerror(errno));
  215. }
  216. } // namespace
  217. BackendFactory &SolarisBackendFactory::getFactory()
  218. {
  219. static SolarisBackendFactory factory{};
  220. return factory;
  221. }
  222. bool SolarisBackendFactory::init()
  223. {
  224. if(auto devopt = ConfigValueStr({}, "solaris", "device"))
  225. solaris_driver = std::move(*devopt);
  226. return true;
  227. }
  228. bool SolarisBackendFactory::querySupport(BackendType type)
  229. { return type == BackendType::Playback; }
  230. auto SolarisBackendFactory::enumerate(BackendType type) -> std::vector<std::string>
  231. {
  232. switch(type)
  233. {
  234. case BackendType::Playback:
  235. if(struct stat buf{}; stat(solaris_driver.c_str(), &buf) == 0)
  236. return std::vector{std::string{GetDefaultName()}};
  237. break;
  238. case BackendType::Capture:
  239. break;
  240. }
  241. return {};
  242. }
  243. BackendPtr SolarisBackendFactory::createBackend(DeviceBase *device, BackendType type)
  244. {
  245. if(type == BackendType::Playback)
  246. return BackendPtr{new SolarisBackend{device}};
  247. return nullptr;
  248. }