solaris.cpp 8.1 KB

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