Enable proper full C++ exception handling on MSVC
[openal-soft.git] / alc / backends / null.cpp
blobd2e036ad9e11eefb18fce5bb2630e31148e60237
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 "almalloc.h"
34 #include "alstring.h"
35 #include "althrd_setname.h"
36 #include "core/device.h"
37 #include "core/helpers.h"
40 namespace {
42 using std::chrono::seconds;
43 using std::chrono::milliseconds;
44 using std::chrono::nanoseconds;
45 using namespace std::string_view_literals;
47 [[nodiscard]] constexpr auto GetDeviceName() noexcept { return "No Output"sv; }
50 struct NullBackend final : public BackendBase {
51 NullBackend(DeviceBase *device) noexcept : BackendBase{device} { }
53 int mixerProc();
55 void open(std::string_view name) override;
56 bool reset() override;
57 void start() override;
58 void stop() override;
60 std::atomic<bool> mKillNow{true};
61 std::thread mThread;
64 int NullBackend::mixerProc()
66 const milliseconds restTime{mDevice->UpdateSize*1000/mDevice->Frequency / 2};
68 SetRTPriority();
69 althrd_setname(GetMixerThreadName());
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(std::string_view name)
110 if(name.empty())
111 name = GetDeviceName();
112 else if(name != GetDeviceName())
113 throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%.*s\" not found",
114 al::sizei(name), name.data()};
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 auto NullBackendFactory::enumerate(BackendType type) -> std::vector<std::string>
155 switch(type)
157 case BackendType::Playback:
158 /* Include null char. */
159 return std::vector{std::string{GetDeviceName()}};
160 case BackendType::Capture:
161 break;
163 return {};
166 BackendPtr NullBackendFactory::createBackend(DeviceBase *device, BackendType type)
168 if(type == BackendType::Playback)
169 return BackendPtr{new NullBackend{device}};
170 return nullptr;
173 BackendFactory &NullBackendFactory::getFactory()
175 static NullBackendFactory factory{};
176 return factory;