🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

303 lines
8.2 KiB

  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 <thread>
  36. #include <functional>
  37. #include "albyte.h"
  38. #include "alc/alconfig.h"
  39. #include "core/device.h"
  40. #include "core/helpers.h"
  41. #include "core/logging.h"
  42. #include "threads.h"
  43. #include "vector.h"
  44. #include <sys/audioio.h>
  45. namespace {
  46. constexpr char solaris_device[] = "Solaris Default";
  47. std::string solaris_driver{"/dev/audio"};
  48. struct SolarisBackend final : public BackendBase {
  49. SolarisBackend(DeviceBase *device) noexcept : BackendBase{device} { }
  50. ~SolarisBackend() override;
  51. int mixerProc();
  52. void open(const char *name) override;
  53. bool reset() override;
  54. void start() override;
  55. void stop() override;
  56. int mFd{-1};
  57. uint mFrameStep{};
  58. al::vector<al::byte> mBuffer;
  59. std::atomic<bool> mKillNow{true};
  60. std::thread mThread;
  61. DEF_NEWDEL(SolarisBackend)
  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(MIXER_THREAD_NAME);
  73. const size_t frame_step{mDevice->channelsFromFmt()};
  74. const uint 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::byte *write_ptr{mBuffer.data()};
  96. size_t to_write{mBuffer.size()};
  97. mDevice->renderSamples(write_ptr, static_cast<uint>(to_write/frame_size), frame_step);
  98. while(to_write > 0 && !mKillNow.load(std::memory_order_acquire))
  99. {
  100. ssize_t wrote{write(mFd, write_ptr, to_write)};
  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. to_write -= static_cast<size_t>(wrote);
  110. write_ptr += wrote;
  111. }
  112. }
  113. return 0;
  114. }
  115. void SolarisBackend::open(const char *name)
  116. {
  117. if(!name)
  118. name = solaris_device;
  119. else if(strcmp(name, solaris_device) != 0)
  120. throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%s\" not found",
  121. name};
  122. int fd{::open(solaris_driver.c_str(), O_WRONLY)};
  123. if(fd == -1)
  124. throw al::backend_exception{al::backend_error::NoDevice, "Could not open %s: %s",
  125. solaris_driver.c_str(), strerror(errno)};
  126. if(mFd != -1)
  127. ::close(mFd);
  128. mFd = fd;
  129. mDevice->DeviceName = name;
  130. }
  131. bool SolarisBackend::reset()
  132. {
  133. audio_info_t info;
  134. AUDIO_INITINFO(&info);
  135. info.play.sample_rate = mDevice->Frequency;
  136. info.play.channels = mDevice->channelsFromFmt();
  137. switch(mDevice->FmtType)
  138. {
  139. case DevFmtByte:
  140. info.play.precision = 8;
  141. info.play.encoding = AUDIO_ENCODING_LINEAR;
  142. break;
  143. case DevFmtUByte:
  144. info.play.precision = 8;
  145. info.play.encoding = AUDIO_ENCODING_LINEAR8;
  146. break;
  147. case DevFmtUShort:
  148. case DevFmtInt:
  149. case DevFmtUInt:
  150. case DevFmtFloat:
  151. mDevice->FmtType = DevFmtShort;
  152. /* fall-through */
  153. case DevFmtShort:
  154. info.play.precision = 16;
  155. info.play.encoding = AUDIO_ENCODING_LINEAR;
  156. break;
  157. }
  158. info.play.buffer_size = mDevice->BufferSize * mDevice->frameSizeFromFmt();
  159. if(ioctl(mFd, AUDIO_SETINFO, &info) < 0)
  160. {
  161. ERR("ioctl failed: %s\n", strerror(errno));
  162. return false;
  163. }
  164. if(mDevice->channelsFromFmt() != info.play.channels)
  165. {
  166. if(info.play.channels >= 2)
  167. mDevice->FmtChans = DevFmtStereo;
  168. else if(info.play.channels == 1)
  169. mDevice->FmtChans = DevFmtMono;
  170. else
  171. throw al::backend_exception{al::backend_error::DeviceError,
  172. "Got %u device channels", info.play.channels};
  173. }
  174. if(info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR8)
  175. mDevice->FmtType = DevFmtUByte;
  176. else if(info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR)
  177. mDevice->FmtType = DevFmtByte;
  178. else if(info.play.precision == 16 && info.play.encoding == AUDIO_ENCODING_LINEAR)
  179. mDevice->FmtType = DevFmtShort;
  180. else if(info.play.precision == 32 && info.play.encoding == AUDIO_ENCODING_LINEAR)
  181. mDevice->FmtType = DevFmtInt;
  182. else
  183. {
  184. ERR("Got unhandled sample type: %d (0x%x)\n", info.play.precision, info.play.encoding);
  185. return false;
  186. }
  187. uint frame_size{mDevice->bytesFromFmt() * info.play.channels};
  188. mFrameStep = info.play.channels;
  189. mDevice->Frequency = info.play.sample_rate;
  190. mDevice->BufferSize = info.play.buffer_size / frame_size;
  191. /* How to get the actual period size/count? */
  192. mDevice->UpdateSize = mDevice->BufferSize / 2;
  193. setDefaultChannelOrder();
  194. mBuffer.resize(mDevice->UpdateSize * size_t{frame_size});
  195. std::fill(mBuffer.begin(), mBuffer.end(), al::byte{});
  196. return true;
  197. }
  198. void SolarisBackend::start()
  199. {
  200. try {
  201. mKillNow.store(false, std::memory_order_release);
  202. mThread = std::thread{std::mem_fn(&SolarisBackend::mixerProc), this};
  203. }
  204. catch(std::exception& e) {
  205. throw al::backend_exception{al::backend_error::DeviceError,
  206. "Failed to start mixing thread: %s", e.what()};
  207. }
  208. }
  209. void SolarisBackend::stop()
  210. {
  211. if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
  212. return;
  213. mThread.join();
  214. if(ioctl(mFd, AUDIO_DRAIN) < 0)
  215. ERR("Error draining device: %s\n", strerror(errno));
  216. }
  217. } // namespace
  218. BackendFactory &SolarisBackendFactory::getFactory()
  219. {
  220. static SolarisBackendFactory factory{};
  221. return factory;
  222. }
  223. bool SolarisBackendFactory::init()
  224. {
  225. if(auto devopt = ConfigValueStr(nullptr, "solaris", "device"))
  226. solaris_driver = std::move(*devopt);
  227. return true;
  228. }
  229. bool SolarisBackendFactory::querySupport(BackendType type)
  230. { return type == BackendType::Playback; }
  231. std::string SolarisBackendFactory::probe(BackendType type)
  232. {
  233. std::string outnames;
  234. switch(type)
  235. {
  236. case BackendType::Playback:
  237. {
  238. struct stat buf;
  239. if(stat(solaris_driver.c_str(), &buf) == 0)
  240. outnames.append(solaris_device, sizeof(solaris_device));
  241. }
  242. break;
  243. case BackendType::Capture:
  244. break;
  245. }
  246. return outnames;
  247. }
  248. BackendPtr SolarisBackendFactory::createBackend(DeviceBase *device, BackendType type)
  249. {
  250. if(type == BackendType::Playback)
  251. return BackendPtr{new SolarisBackend{device}};
  252. return nullptr;
  253. }