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.
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.
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
25 #include <sys/ioctl.h>
26 #include <sys/types.h>
43 #include "alc/alconfig.h"
44 #include "core/device.h"
45 #include "core/helpers.h"
46 #include "core/logging.h"
50 #include <sys/audioio.h>
55 constexpr char solaris_device[] = "Solaris Default";
57 std::string solaris_driver{"/dev/audio"};
60 struct SolarisBackend final : public BackendBase {
61 SolarisBackend(DeviceBase *device) noexcept : BackendBase{device} { }
62 ~SolarisBackend() override;
66 void open(const char *name) override;
67 bool reset() override;
68 void start() override;
74 al::vector<al::byte> mBuffer;
76 std::atomic<bool> mKillNow{true};
79 DEF_NEWDEL(SolarisBackend)
82 SolarisBackend::~SolarisBackend()
89 int SolarisBackend::mixerProc()
92 althrd_setname(MIXER_THREAD_NAME);
94 const size_t frame_step{mDevice->channelsFromFmt()};
95 const uint frame_size{mDevice->frameSizeFromFmt()};
97 while(!mKillNow.load(std::memory_order_acquire)
98 && mDevice->Connected.load(std::memory_order_acquire))
102 pollitem.events = POLLOUT;
104 int pret{poll(&pollitem, 1, 1000)};
107 if(errno == EINTR || errno == EAGAIN)
109 ERR("poll failed: %s\n", strerror(errno));
110 mDevice->handleDisconnect("Failed to wait for playback buffer: %s", strerror(errno));
115 WARN("poll timeout\n");
119 al::byte *write_ptr{mBuffer.data()};
120 size_t to_write{mBuffer.size()};
121 mDevice->renderSamples(write_ptr, static_cast<uint>(to_write/frame_size), frame_step);
122 while(to_write > 0 && !mKillNow.load(std::memory_order_acquire))
124 ssize_t wrote{write(mFd, write_ptr, to_write)};
127 if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
129 ERR("write failed: %s\n", strerror(errno));
130 mDevice->handleDisconnect("Failed to write playback samples: %s", strerror(errno));
134 to_write -= static_cast<size_t>(wrote);
143 void SolarisBackend::open(const char *name)
146 name = solaris_device;
147 else if(strcmp(name, solaris_device) != 0)
148 throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%s\" not found",
151 int fd{::open(solaris_driver.c_str(), O_WRONLY)};
153 throw al::backend_exception{al::backend_error::NoDevice, "Could not open %s: %s",
154 solaris_driver.c_str(), strerror(errno)};
160 mDevice->DeviceName = name;
163 bool SolarisBackend::reset()
166 AUDIO_INITINFO(&info);
168 info.play.sample_rate = mDevice->Frequency;
169 info.play.channels = mDevice->channelsFromFmt();
170 switch(mDevice->FmtType)
173 info.play.precision = 8;
174 info.play.encoding = AUDIO_ENCODING_LINEAR;
177 info.play.precision = 8;
178 info.play.encoding = AUDIO_ENCODING_LINEAR8;
184 mDevice->FmtType = DevFmtShort;
187 info.play.precision = 16;
188 info.play.encoding = AUDIO_ENCODING_LINEAR;
191 info.play.buffer_size = mDevice->BufferSize * mDevice->frameSizeFromFmt();
193 if(ioctl(mFd, AUDIO_SETINFO, &info) < 0)
195 ERR("ioctl failed: %s\n", strerror(errno));
199 if(mDevice->channelsFromFmt() != info.play.channels)
201 if(info.play.channels >= 2)
202 mDevice->FmtChans = DevFmtStereo;
203 else if(info.play.channels == 1)
204 mDevice->FmtChans = DevFmtMono;
206 throw al::backend_exception{al::backend_error::DeviceError,
207 "Got %u device channels", info.play.channels};
210 if(info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR8)
211 mDevice->FmtType = DevFmtUByte;
212 else if(info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR)
213 mDevice->FmtType = DevFmtByte;
214 else if(info.play.precision == 16 && info.play.encoding == AUDIO_ENCODING_LINEAR)
215 mDevice->FmtType = DevFmtShort;
216 else if(info.play.precision == 32 && info.play.encoding == AUDIO_ENCODING_LINEAR)
217 mDevice->FmtType = DevFmtInt;
220 ERR("Got unhandled sample type: %d (0x%x)\n", info.play.precision, info.play.encoding);
224 uint frame_size{mDevice->bytesFromFmt() * info.play.channels};
225 mFrameStep = info.play.channels;
226 mDevice->Frequency = info.play.sample_rate;
227 mDevice->BufferSize = info.play.buffer_size / frame_size;
228 /* How to get the actual period size/count? */
229 mDevice->UpdateSize = mDevice->BufferSize / 2;
231 setDefaultChannelOrder();
233 mBuffer.resize(mDevice->UpdateSize * size_t{frame_size});
234 std::fill(mBuffer.begin(), mBuffer.end(), al::byte{});
239 void SolarisBackend::start()
242 mKillNow.store(false, std::memory_order_release);
243 mThread = std::thread{std::mem_fn(&SolarisBackend::mixerProc), this};
245 catch(std::exception& e) {
246 throw al::backend_exception{al::backend_error::DeviceError,
247 "Failed to start mixing thread: %s", e.what()};
251 void SolarisBackend::stop()
253 if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
257 if(ioctl(mFd, AUDIO_DRAIN) < 0)
258 ERR("Error draining device: %s\n", strerror(errno));
263 BackendFactory &SolarisBackendFactory::getFactory()
265 static SolarisBackendFactory factory{};
269 bool SolarisBackendFactory::init()
271 if(auto devopt = ConfigValueStr(nullptr, "solaris", "device"))
272 solaris_driver = std::move(*devopt);
276 bool SolarisBackendFactory::querySupport(BackendType type)
277 { return type == BackendType::Playback; }
279 std::string SolarisBackendFactory::probe(BackendType type)
281 std::string outnames;
284 case BackendType::Playback:
287 if(stat(solaris_driver.c_str(), &buf) == 0)
288 outnames.append(solaris_device, sizeof(solaris_device));
292 case BackendType::Capture:
298 BackendPtr SolarisBackendFactory::createBackend(DeviceBase *device, BackendType type)
300 if(type == BackendType::Playback)
301 return BackendPtr{new SolarisBackend{device}};