Avoid a stateful unique_ptr deleter
[openal-soft.git] / alc / backends / null.cpp
blob5a8fc255c19ebebca54c78ccdaac2c55796c8d5b
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 2010 by Chris Robinson
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 "null.h"
25 #include <exception>
26 #include <atomic>
27 #include <chrono>
28 #include <cstdint>
29 #include <cstring>
30 #include <functional>
31 #include <thread>
33 #include "core/device.h"
34 #include "almalloc.h"
35 #include "core/helpers.h"
36 #include "threads.h"
39 namespace {
41 using std::chrono::seconds;
42 using std::chrono::milliseconds;
43 using std::chrono::nanoseconds;
45 constexpr char nullDevice[] = "No Output";
48 struct NullBackend final : public BackendBase {
49 NullBackend(DeviceBase *device) noexcept : BackendBase{device} { }
51 int mixerProc();
53 void open(const char *name) override;
54 bool reset() override;
55 void start() override;
56 void stop() override;
58 std::atomic<bool> mKillNow{true};
59 std::thread mThread;
61 DEF_NEWDEL(NullBackend)
64 int NullBackend::mixerProc()
66 const milliseconds restTime{mDevice->UpdateSize*1000/mDevice->Frequency / 2};
68 SetRTPriority();
69 althrd_setname(MIXER_THREAD_NAME);
71 int64_t done{0};
72 auto start = std::chrono::steady_clock::now();
73 while(!mKillNow.load(std::memory_order_acquire)
74 && mDevice->Connected.load(std::memory_order_acquire))
76 auto now = std::chrono::steady_clock::now();
78 /* This converts from nanoseconds to nanosamples, then to samples. */
79 int64_t avail{std::chrono::duration_cast<seconds>((now-start) * mDevice->Frequency).count()};
80 if(avail-done < mDevice->UpdateSize)
82 std::this_thread::sleep_for(restTime);
83 continue;
85 while(avail-done >= mDevice->UpdateSize)
87 mDevice->renderSamples(nullptr, mDevice->UpdateSize, 0u);
88 done += mDevice->UpdateSize;
91 /* For every completed second, increment the start time and reduce the
92 * samples done. This prevents the difference between the start time
93 * and current time from growing too large, while maintaining the
94 * correct number of samples to render.
96 if(done >= mDevice->Frequency)
98 seconds s{done/mDevice->Frequency};
99 start += s;
100 done -= mDevice->Frequency*s.count();
104 return 0;
108 void NullBackend::open(const char *name)
110 if(!name)
111 name = nullDevice;
112 else if(strcmp(name, nullDevice) != 0)
113 throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%s\" not found",
114 name};
116 mDevice->DeviceName = name;
119 bool NullBackend::reset()
121 setDefaultWFXChannelOrder();
122 return true;
125 void NullBackend::start()
127 try {
128 mKillNow.store(false, std::memory_order_release);
129 mThread = std::thread{std::mem_fn(&NullBackend::mixerProc), this};
131 catch(std::exception& e) {
132 throw al::backend_exception{al::backend_error::DeviceError,
133 "Failed to start mixing thread: %s", e.what()};
137 void NullBackend::stop()
139 if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
140 return;
141 mThread.join();
144 } // namespace
147 bool NullBackendFactory::init()
148 { return true; }
150 bool NullBackendFactory::querySupport(BackendType type)
151 { return (type == BackendType::Playback); }
153 std::string NullBackendFactory::probe(BackendType type)
155 std::string outnames;
156 switch(type)
158 case BackendType::Playback:
159 /* Includes null char. */
160 outnames.append(nullDevice, sizeof(nullDevice));
161 break;
162 case BackendType::Capture:
163 break;
165 return outnames;
168 BackendPtr NullBackendFactory::createBackend(DeviceBase *device, BackendType type)
170 if(type == BackendType::Playback)
171 return BackendPtr{new NullBackend{device}};
172 return nullptr;
175 BackendFactory &NullBackendFactory::getFactory()
177 static NullBackendFactory factory{};
178 return factory;