Remove some unnecessary uses of mem_fn
[openal-soft.git] / alc / backends / null.cpp
blobec1e2a2c590fdd5e66b58021cd0575b74e2e8bc4
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 <thread>
32 #include "althrd_setname.h"
33 #include "core/device.h"
34 #include "core/helpers.h"
37 namespace {
39 using std::chrono::seconds;
40 using std::chrono::milliseconds;
41 using std::chrono::nanoseconds;
42 using namespace std::string_view_literals;
44 [[nodiscard]] constexpr auto GetDeviceName() noexcept { return "No Output"sv; }
47 struct NullBackend final : public BackendBase {
48 NullBackend(DeviceBase *device) noexcept : BackendBase{device} { }
50 int mixerProc();
52 void open(std::string_view name) override;
53 bool reset() override;
54 void start() override;
55 void stop() override;
57 std::atomic<bool> mKillNow{true};
58 std::thread mThread;
61 int NullBackend::mixerProc()
63 const milliseconds restTime{mDevice->UpdateSize*1000/mDevice->Frequency / 2};
65 SetRTPriority();
66 althrd_setname(GetMixerThreadName());
68 int64_t done{0};
69 auto start = std::chrono::steady_clock::now();
70 while(!mKillNow.load(std::memory_order_acquire)
71 && mDevice->Connected.load(std::memory_order_acquire))
73 auto now = std::chrono::steady_clock::now();
75 /* This converts from nanoseconds to nanosamples, then to samples. */
76 int64_t avail{std::chrono::duration_cast<seconds>((now-start) * mDevice->Frequency).count()};
77 if(avail-done < mDevice->UpdateSize)
79 std::this_thread::sleep_for(restTime);
80 continue;
82 while(avail-done >= mDevice->UpdateSize)
84 mDevice->renderSamples(nullptr, mDevice->UpdateSize, 0u);
85 done += mDevice->UpdateSize;
88 /* For every completed second, increment the start time and reduce the
89 * samples done. This prevents the difference between the start time
90 * and current time from growing too large, while maintaining the
91 * correct number of samples to render.
93 if(done >= mDevice->Frequency)
95 seconds s{done/mDevice->Frequency};
96 start += s;
97 done -= mDevice->Frequency*s.count();
101 return 0;
105 void NullBackend::open(std::string_view name)
107 if(name.empty())
108 name = GetDeviceName();
109 else if(name != GetDeviceName())
110 throw al::backend_exception{al::backend_error::NoDevice, "Device name \"{}\" not found",
111 name};
113 mDeviceName = name;
116 bool NullBackend::reset()
118 setDefaultWFXChannelOrder();
119 return true;
122 void NullBackend::start()
124 try {
125 mKillNow.store(false, std::memory_order_release);
126 mThread = std::thread{&NullBackend::mixerProc, this};
128 catch(std::exception& e) {
129 throw al::backend_exception{al::backend_error::DeviceError,
130 "Failed to start mixing thread: {}", e.what()};
134 void NullBackend::stop()
136 if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
137 return;
138 mThread.join();
141 } // namespace
144 bool NullBackendFactory::init()
145 { return true; }
147 bool NullBackendFactory::querySupport(BackendType type)
148 { return (type == BackendType::Playback); }
150 auto NullBackendFactory::enumerate(BackendType type) -> std::vector<std::string>
152 switch(type)
154 case BackendType::Playback:
155 /* Include null char. */
156 return std::vector{std::string{GetDeviceName()}};
157 case BackendType::Capture:
158 break;
160 return {};
163 BackendPtr NullBackendFactory::createBackend(DeviceBase *device, BackendType type)
165 if(type == BackendType::Playback)
166 return BackendPtr{new NullBackend{device}};
167 return nullptr;
170 BackendFactory &NullBackendFactory::getFactory()
172 static NullBackendFactory factory{};
173 return factory;