Avoid class templates for the POPCNT64/CTZ64 macros
[openal-soft.git] / alc / backends / null.cpp
blobf78a23a0bdc20ce3c9d93b9b353375587e07fc35
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 "backends/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 "alcmain.h"
34 #include "alexcpt.h"
35 #include "almalloc.h"
36 #include "alu.h"
37 #include "logging.h"
38 #include "threads.h"
41 namespace {
43 using std::chrono::seconds;
44 using std::chrono::milliseconds;
45 using std::chrono::nanoseconds;
47 constexpr ALCchar nullDevice[] = "No Output";
50 struct NullBackend final : public BackendBase {
51 NullBackend(ALCdevice *device) noexcept : BackendBase{device} { }
53 int mixerProc();
55 void open(const ALCchar *name) override;
56 bool reset() override;
57 void start() override;
58 void stop() override;
60 std::atomic<bool> mKillNow{true};
61 std::thread mThread;
63 DEF_NEWDEL(NullBackend)
66 int NullBackend::mixerProc()
68 const milliseconds restTime{mDevice->UpdateSize*1000/mDevice->Frequency / 2};
70 SetRTPriority();
71 althrd_setname(MIXER_THREAD_NAME);
73 int64_t done{0};
74 auto start = std::chrono::steady_clock::now();
75 while(!mKillNow.load(std::memory_order_acquire)
76 && mDevice->Connected.load(std::memory_order_acquire))
78 auto now = std::chrono::steady_clock::now();
80 /* This converts from nanoseconds to nanosamples, then to samples. */
81 int64_t avail{std::chrono::duration_cast<seconds>((now-start) * mDevice->Frequency).count()};
82 if(avail-done < mDevice->UpdateSize)
84 std::this_thread::sleep_for(restTime);
85 continue;
87 while(avail-done >= mDevice->UpdateSize)
89 mDevice->renderSamples(nullptr, mDevice->UpdateSize, 0u);
90 done += mDevice->UpdateSize;
93 /* For every completed second, increment the start time and reduce the
94 * samples done. This prevents the difference between the start time
95 * and current time from growing too large, while maintaining the
96 * correct number of samples to render.
98 if(done >= mDevice->Frequency)
100 seconds s{done/mDevice->Frequency};
101 start += s;
102 done -= mDevice->Frequency*s.count();
106 return 0;
110 void NullBackend::open(const ALCchar *name)
112 if(!name)
113 name = nullDevice;
114 else if(strcmp(name, nullDevice) != 0)
115 throw al::backend_exception{ALC_INVALID_VALUE, "Device name \"%s\" not found", name};
117 mDevice->DeviceName = name;
120 bool NullBackend::reset()
122 setDefaultWFXChannelOrder();
123 return true;
126 void NullBackend::start()
128 try {
129 mKillNow.store(false, std::memory_order_release);
130 mThread = std::thread{std::mem_fn(&NullBackend::mixerProc), this};
132 catch(std::exception& e) {
133 throw al::backend_exception{ALC_INVALID_DEVICE, "Failed to start mixing thread: %s",
134 e.what()};
138 void NullBackend::stop()
140 if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
141 return;
142 mThread.join();
145 } // namespace
148 bool NullBackendFactory::init()
149 { return true; }
151 bool NullBackendFactory::querySupport(BackendType type)
152 { return (type == BackendType::Playback); }
154 std::string NullBackendFactory::probe(BackendType type)
156 std::string outnames;
157 switch(type)
159 case BackendType::Playback:
160 /* Includes null char. */
161 outnames.append(nullDevice, sizeof(nullDevice));
162 break;
163 case BackendType::Capture:
164 break;
166 return outnames;
169 BackendPtr NullBackendFactory::createBackend(ALCdevice *device, BackendType type)
171 if(type == BackendType::Playback)
172 return BackendPtr{new NullBackend{device}};
173 return nullptr;
176 BackendFactory &NullBackendFactory::getFactory()
178 static NullBackendFactory factory{};
179 return factory;