Avoid using 'using' for specific operators
[openal-soft.git] / alc / backends / null.cpp
blobc96d76ef7ee312c76ab36191e4e1cb0f687698c6
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 "althrd_setname.h"
34 #include "almalloc.h"
35 #include "core/device.h"
36 #include "core/helpers.h"
39 namespace {
41 using std::chrono::seconds;
42 using std::chrono::milliseconds;
43 using std::chrono::nanoseconds;
44 using namespace std::string_view_literals;
46 [[nodiscard]] constexpr auto GetDeviceName() noexcept { return "No Output"sv; }
49 struct NullBackend final : public BackendBase {
50 NullBackend(DeviceBase *device) noexcept : BackendBase{device} { }
52 int mixerProc();
54 void open(std::string_view name) override;
55 bool reset() override;
56 void start() override;
57 void stop() override;
59 std::atomic<bool> mKillNow{true};
60 std::thread mThread;
63 int NullBackend::mixerProc()
65 const milliseconds restTime{mDevice->UpdateSize*1000/mDevice->Frequency / 2};
67 SetRTPriority();
68 althrd_setname(MIXER_THREAD_NAME);
70 int64_t done{0};
71 auto start = std::chrono::steady_clock::now();
72 while(!mKillNow.load(std::memory_order_acquire)
73 && mDevice->Connected.load(std::memory_order_acquire))
75 auto now = std::chrono::steady_clock::now();
77 /* This converts from nanoseconds to nanosamples, then to samples. */
78 int64_t avail{std::chrono::duration_cast<seconds>((now-start) * mDevice->Frequency).count()};
79 if(avail-done < mDevice->UpdateSize)
81 std::this_thread::sleep_for(restTime);
82 continue;
84 while(avail-done >= mDevice->UpdateSize)
86 mDevice->renderSamples(nullptr, mDevice->UpdateSize, 0u);
87 done += mDevice->UpdateSize;
90 /* For every completed second, increment the start time and reduce the
91 * samples done. This prevents the difference between the start time
92 * and current time from growing too large, while maintaining the
93 * correct number of samples to render.
95 if(done >= mDevice->Frequency)
97 seconds s{done/mDevice->Frequency};
98 start += s;
99 done -= mDevice->Frequency*s.count();
103 return 0;
107 void NullBackend::open(std::string_view name)
109 if(name.empty())
110 name = GetDeviceName();
111 else if(name != GetDeviceName())
112 throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%.*s\" not found",
113 static_cast<int>(name.length()), name.data()};
115 mDevice->DeviceName = name;
118 bool NullBackend::reset()
120 setDefaultWFXChannelOrder();
121 return true;
124 void NullBackend::start()
126 try {
127 mKillNow.store(false, std::memory_order_release);
128 mThread = std::thread{std::mem_fn(&NullBackend::mixerProc), this};
130 catch(std::exception& e) {
131 throw al::backend_exception{al::backend_error::DeviceError,
132 "Failed to start mixing thread: %s", e.what()};
136 void NullBackend::stop()
138 if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
139 return;
140 mThread.join();
143 } // namespace
146 bool NullBackendFactory::init()
147 { return true; }
149 bool NullBackendFactory::querySupport(BackendType type)
150 { return (type == BackendType::Playback); }
152 std::string NullBackendFactory::probe(BackendType type)
154 switch(type)
156 case BackendType::Playback:
157 /* Include null char. */
158 return std::string{GetDeviceName()} + '\0';
159 case BackendType::Capture:
160 break;
162 return std::string{};
165 BackendPtr NullBackendFactory::createBackend(DeviceBase *device, BackendType type)
167 if(type == BackendType::Playback)
168 return BackendPtr{new NullBackend{device}};
169 return nullptr;
172 BackendFactory &NullBackendFactory::getFactory()
174 static NullBackendFactory factory{};
175 return factory;