Avoid a stateful unique_ptr deleter
[openal-soft.git] / alc / backends / sndio.cpp
blob469e5c85ae25a75f38b39fe47257bcba2326c649
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.
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
21 #include "config.h"
23 #include "sndio.h"
25 #include <poll.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
30 #include <thread>
31 #include <functional>
33 #include "alnumeric.h"
34 #include "core/device.h"
35 #include "core/helpers.h"
36 #include "core/logging.h"
37 #include "ringbuffer.h"
38 #include "threads.h"
39 #include "vector.h"
41 #include <sndio.h>
44 namespace {
46 static const char sndio_device[] = "SndIO Default";
48 struct SioPar : public sio_par {
49 SioPar() { sio_initpar(this); }
51 void clear() { sio_initpar(this); }
54 struct SndioPlayback final : public BackendBase {
55 SndioPlayback(DeviceBase *device) noexcept : BackendBase{device} { }
56 ~SndioPlayback() override;
58 int mixerProc();
60 void open(const char *name) override;
61 bool reset() override;
62 void start() override;
63 void stop() override;
65 sio_hdl *mSndHandle{nullptr};
66 uint mFrameStep{};
68 al::vector<al::byte> mBuffer;
70 std::atomic<bool> mKillNow{true};
71 std::thread mThread;
73 DEF_NEWDEL(SndioPlayback)
76 SndioPlayback::~SndioPlayback()
78 if(mSndHandle)
79 sio_close(mSndHandle);
80 mSndHandle = nullptr;
83 int SndioPlayback::mixerProc()
85 const size_t frameStep{mFrameStep};
86 const size_t frameSize{frameStep * mDevice->bytesFromFmt()};
88 SetRTPriority();
89 althrd_setname(MIXER_THREAD_NAME);
91 while(!mKillNow.load(std::memory_order_acquire)
92 && mDevice->Connected.load(std::memory_order_acquire))
94 al::span<al::byte> buffer{mBuffer};
96 mDevice->renderSamples(buffer.data(), static_cast<uint>(buffer.size() / frameSize),
97 frameStep);
98 while(!buffer.empty() && !mKillNow.load(std::memory_order_acquire))
100 size_t wrote{sio_write(mSndHandle, buffer.data(), buffer.size())};
101 if(wrote == 0)
103 ERR("sio_write failed\n");
104 mDevice->handleDisconnect("Failed to write playback samples");
105 break;
107 buffer = buffer.subspan(wrote);
111 return 0;
115 void SndioPlayback::open(const char *name)
117 if(!name)
118 name = sndio_device;
119 else if(strcmp(name, sndio_device) != 0)
120 throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%s\" not found",
121 name};
123 sio_hdl *sndHandle{sio_open(nullptr, SIO_PLAY, 0)};
124 if(!sndHandle)
125 throw al::backend_exception{al::backend_error::NoDevice, "Could not open backend device"};
127 if(mSndHandle)
128 sio_close(mSndHandle);
129 mSndHandle = sndHandle;
131 mDevice->DeviceName = name;
134 bool SndioPlayback::reset()
136 SioPar par;
138 auto tryfmt = mDevice->FmtType;
139 retry_params:
140 switch(tryfmt)
142 case DevFmtByte:
143 par.bits = 8;
144 par.sig = 1;
145 break;
146 case DevFmtUByte:
147 par.bits = 8;
148 par.sig = 0;
149 break;
150 case DevFmtShort:
151 par.bits = 16;
152 par.sig = 1;
153 break;
154 case DevFmtUShort:
155 par.bits = 16;
156 par.sig = 0;
157 break;
158 case DevFmtFloat:
159 case DevFmtInt:
160 par.bits = 32;
161 par.sig = 1;
162 break;
163 case DevFmtUInt:
164 par.bits = 32;
165 par.sig = 0;
166 break;
168 par.bps = SIO_BPS(par.bits);
169 par.le = SIO_LE_NATIVE;
170 par.msb = 1;
172 par.rate = mDevice->Frequency;
173 par.pchan = mDevice->channelsFromFmt();
175 par.round = mDevice->UpdateSize;
176 par.appbufsz = mDevice->BufferSize - mDevice->UpdateSize;
177 if(!par.appbufsz) par.appbufsz = mDevice->UpdateSize;
179 try {
180 if(!sio_setpar(mSndHandle, &par))
181 throw al::backend_exception{al::backend_error::DeviceError,
182 "Failed to set device parameters"};
184 par.clear();
185 if(!sio_getpar(mSndHandle, &par))
186 throw al::backend_exception{al::backend_error::DeviceError,
187 "Failed to get device parameters"};
189 if(par.bps > 1 && par.le != SIO_LE_NATIVE)
190 throw al::backend_exception{al::backend_error::DeviceError,
191 "%s-endian samples not supported", par.le ? "Little" : "Big"};
192 if(par.bits < par.bps*8 && !par.msb)
193 throw al::backend_exception{al::backend_error::DeviceError,
194 "MSB-padded samples not supported (%u of %u bits)", par.bits, par.bps*8};
195 if(par.pchan < 1)
196 throw al::backend_exception{al::backend_error::DeviceError,
197 "No playback channels on device"};
199 catch(al::backend_exception &e) {
200 if(tryfmt == DevFmtShort)
201 throw;
202 par.clear();
203 tryfmt = DevFmtShort;
204 goto retry_params;
207 if(par.bps == 1)
208 mDevice->FmtType = (par.sig==1) ? DevFmtByte : DevFmtUByte;
209 else if(par.bps == 2)
210 mDevice->FmtType = (par.sig==1) ? DevFmtShort : DevFmtUShort;
211 else if(par.bps == 4)
212 mDevice->FmtType = (par.sig==1) ? DevFmtInt : DevFmtUInt;
213 else
214 throw al::backend_exception{al::backend_error::DeviceError,
215 "Unhandled sample format: %s %u-bit", (par.sig?"signed":"unsigned"), par.bps*8};
217 mFrameStep = par.pchan;
218 if(par.pchan != mDevice->channelsFromFmt())
220 WARN("Got %u channel%s for %s\n", par.pchan, (par.pchan==1)?"":"s",
221 DevFmtChannelsString(mDevice->FmtChans));
222 if(par.pchan < 2) mDevice->FmtChans = DevFmtMono;
223 else mDevice->FmtChans = DevFmtStereo;
225 mDevice->Frequency = par.rate;
227 setDefaultChannelOrder();
229 mDevice->UpdateSize = par.round;
230 mDevice->BufferSize = par.bufsz + par.round;
232 mBuffer.resize(mDevice->UpdateSize * par.pchan*par.bps);
233 if(par.sig == 1)
234 std::fill(mBuffer.begin(), mBuffer.end(), al::byte{});
235 else if(par.bits == 8)
236 std::fill_n(mBuffer.data(), mBuffer.size(), al::byte(0x80));
237 else if(par.bits == 16)
238 std::fill_n(reinterpret_cast<uint16_t*>(mBuffer.data()), mBuffer.size()/2, 0x8000);
239 else if(par.bits == 32)
240 std::fill_n(reinterpret_cast<uint32_t*>(mBuffer.data()), mBuffer.size()/4, 0x80000000u);
242 return true;
245 void SndioPlayback::start()
247 if(!sio_start(mSndHandle))
248 throw al::backend_exception{al::backend_error::DeviceError, "Error starting playback"};
250 try {
251 mKillNow.store(false, std::memory_order_release);
252 mThread = std::thread{std::mem_fn(&SndioPlayback::mixerProc), this};
254 catch(std::exception& e) {
255 sio_stop(mSndHandle);
256 throw al::backend_exception{al::backend_error::DeviceError,
257 "Failed to start mixing thread: %s", e.what()};
261 void SndioPlayback::stop()
263 if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
264 return;
265 mThread.join();
267 if(!sio_stop(mSndHandle))
268 ERR("Error stopping device\n");
272 /* TODO: This could be improved by avoiding the ring buffer and record thread,
273 * counting the available samples with the sio_onmove callback and reading
274 * directly from the device. However, this depends on reasonable support for
275 * capture buffer sizes apps may request.
277 struct SndioCapture final : public BackendBase {
278 SndioCapture(DeviceBase *device) noexcept : BackendBase{device} { }
279 ~SndioCapture() override;
281 int recordProc();
283 void open(const char *name) override;
284 void start() override;
285 void stop() override;
286 void captureSamples(al::byte *buffer, uint samples) override;
287 uint availableSamples() override;
289 sio_hdl *mSndHandle{nullptr};
291 RingBufferPtr mRing;
293 std::atomic<bool> mKillNow{true};
294 std::thread mThread;
296 DEF_NEWDEL(SndioCapture)
299 SndioCapture::~SndioCapture()
301 if(mSndHandle)
302 sio_close(mSndHandle);
303 mSndHandle = nullptr;
306 int SndioCapture::recordProc()
308 SetRTPriority();
309 althrd_setname(RECORD_THREAD_NAME);
311 const uint frameSize{mDevice->frameSizeFromFmt()};
313 int nfds_pre{sio_nfds(mSndHandle)};
314 if(nfds_pre <= 0)
316 mDevice->handleDisconnect("Incorrect return value from sio_nfds(): %d", nfds_pre);
317 return 1;
320 auto fds = std::make_unique<pollfd[]>(static_cast<uint>(nfds_pre));
322 while(!mKillNow.load(std::memory_order_acquire)
323 && mDevice->Connected.load(std::memory_order_acquire))
325 /* Wait until there's some samples to read. */
326 const int nfds{sio_pollfd(mSndHandle, fds.get(), POLLIN)};
327 if(nfds <= 0)
329 mDevice->handleDisconnect("Failed to get polling fds: %d", nfds);
330 break;
332 int pollres{::poll(fds.get(), static_cast<uint>(nfds), 2000)};
333 if(pollres < 0)
335 if(errno == EINTR) continue;
336 mDevice->handleDisconnect("Poll error: %s", strerror(errno));
337 break;
339 if(pollres == 0)
340 continue;
342 const int revents{sio_revents(mSndHandle, fds.get())};
343 if((revents&POLLHUP))
345 mDevice->handleDisconnect("Got POLLHUP from poll events");
346 break;
348 if(!(revents&POLLIN))
349 continue;
351 auto data = mRing->getWriteVector();
352 al::span<al::byte> buffer{data.first.buf, data.first.len*frameSize};
353 while(!buffer.empty())
355 size_t got{sio_read(mSndHandle, buffer.data(), buffer.size())};
356 if(got == 0) break;
358 mRing->writeAdvance(got / frameSize);
359 buffer = buffer.subspan(got);
360 if(buffer.empty())
362 data = mRing->getWriteVector();
363 buffer = {data.first.buf, data.first.len*frameSize};
366 if(buffer.empty())
368 /* Got samples to read, but no place to store it. Drop it. */
369 static char junk[4096];
370 sio_read(mSndHandle, junk, sizeof(junk) - (sizeof(junk)%frameSize));
374 return 0;
378 void SndioCapture::open(const char *name)
380 if(!name)
381 name = sndio_device;
382 else if(strcmp(name, sndio_device) != 0)
383 throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%s\" not found",
384 name};
386 mSndHandle = sio_open(nullptr, SIO_REC, true);
387 if(mSndHandle == nullptr)
388 throw al::backend_exception{al::backend_error::NoDevice, "Could not open backend device"};
390 SioPar par;
391 switch(mDevice->FmtType)
393 case DevFmtByte:
394 par.bits = 8;
395 par.sig = 1;
396 break;
397 case DevFmtUByte:
398 par.bits = 8;
399 par.sig = 0;
400 break;
401 case DevFmtShort:
402 par.bits = 16;
403 par.sig = 1;
404 break;
405 case DevFmtUShort:
406 par.bits = 16;
407 par.sig = 0;
408 break;
409 case DevFmtInt:
410 par.bits = 32;
411 par.sig = 1;
412 break;
413 case DevFmtUInt:
414 par.bits = 32;
415 par.sig = 0;
416 break;
417 case DevFmtFloat:
418 throw al::backend_exception{al::backend_error::DeviceError,
419 "%s capture samples not supported", DevFmtTypeString(mDevice->FmtType)};
421 par.bps = SIO_BPS(par.bits);
422 par.le = SIO_LE_NATIVE;
423 par.msb = 1;
424 par.rchan = mDevice->channelsFromFmt();
425 par.rate = mDevice->Frequency;
427 par.appbufsz = maxu(mDevice->BufferSize, mDevice->Frequency/10);
428 par.round = minu(par.appbufsz/2, mDevice->Frequency/40);
430 if(!sio_setpar(mSndHandle, &par) || !sio_getpar(mSndHandle, &par))
431 throw al::backend_exception{al::backend_error::DeviceError,
432 "Failed to set device praameters"};
434 if(par.bps > 1 && par.le != SIO_LE_NATIVE)
435 throw al::backend_exception{al::backend_error::DeviceError,
436 "%s-endian samples not supported", par.le ? "Little" : "Big"};
437 if(par.bits < par.bps*8 && !par.msb)
438 throw al::backend_exception{al::backend_error::DeviceError,
439 "Padded samples not supported (got %u of %u bits)", par.bits, par.bps*8};
441 auto match_fmt = [](DevFmtType fmttype, const sio_par &par) -> bool
443 return (fmttype == DevFmtByte && par.bps == 1 && par.sig != 0)
444 || (fmttype == DevFmtUByte && par.bps == 1 && par.sig == 0)
445 || (fmttype == DevFmtShort && par.bps == 2 && par.sig != 0)
446 || (fmttype == DevFmtUShort && par.bps == 2 && par.sig == 0)
447 || (fmttype == DevFmtInt && par.bps == 4 && par.sig != 0)
448 || (fmttype == DevFmtUInt && par.bps == 4 && par.sig == 0);
450 if(!match_fmt(mDevice->FmtType, par) || mDevice->channelsFromFmt() != par.rchan
451 || mDevice->Frequency != par.rate)
452 throw al::backend_exception{al::backend_error::DeviceError,
453 "Failed to set format %s %s %uhz, got %c%u %u-channel %uhz instead",
454 DevFmtTypeString(mDevice->FmtType), DevFmtChannelsString(mDevice->FmtChans),
455 mDevice->Frequency, par.sig?'s':'u', par.bps*8, par.rchan, par.rate};
457 mRing = RingBuffer::Create(mDevice->BufferSize, par.bps*par.rchan, false);
458 mDevice->BufferSize = static_cast<uint>(mRing->writeSpace());
459 mDevice->UpdateSize = par.round;
461 setDefaultChannelOrder();
463 mDevice->DeviceName = name;
466 void SndioCapture::start()
468 if(!sio_start(mSndHandle))
469 throw al::backend_exception{al::backend_error::DeviceError, "Error starting capture"};
471 try {
472 mKillNow.store(false, std::memory_order_release);
473 mThread = std::thread{std::mem_fn(&SndioCapture::recordProc), this};
475 catch(std::exception& e) {
476 sio_stop(mSndHandle);
477 throw al::backend_exception{al::backend_error::DeviceError,
478 "Failed to start capture thread: %s", e.what()};
482 void SndioCapture::stop()
484 if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
485 return;
486 mThread.join();
488 if(!sio_stop(mSndHandle))
489 ERR("Error stopping device\n");
492 void SndioCapture::captureSamples(al::byte *buffer, uint samples)
493 { mRing->read(buffer, samples); }
495 uint SndioCapture::availableSamples()
496 { return static_cast<uint>(mRing->readSpace()); }
498 } // namespace
500 BackendFactory &SndIOBackendFactory::getFactory()
502 static SndIOBackendFactory factory{};
503 return factory;
506 bool SndIOBackendFactory::init()
507 { return true; }
509 bool SndIOBackendFactory::querySupport(BackendType type)
510 { return (type == BackendType::Playback || type == BackendType::Capture); }
512 std::string SndIOBackendFactory::probe(BackendType type)
514 std::string outnames;
515 switch(type)
517 case BackendType::Playback:
518 case BackendType::Capture:
519 /* Includes null char. */
520 outnames.append(sndio_device, sizeof(sndio_device));
521 break;
523 return outnames;
526 BackendPtr SndIOBackendFactory::createBackend(DeviceBase *device, BackendType type)
528 if(type == BackendType::Playback)
529 return BackendPtr{new SndioPlayback{device}};
530 if(type == BackendType::Capture)
531 return BackendPtr{new SndioCapture{device}};
532 return nullptr;