Use exceptions for backend open failures
[openal-soft.git] / alc / backends / null.cpp
blobaca59605b446f52860d52a3d34a80265a3f71109
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 bool 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 lock();
90 aluMixData(mDevice, nullptr, mDevice->UpdateSize);
91 unlock();
92 done += mDevice->UpdateSize;
95 /* For every completed second, increment the start time and reduce the
96 * samples done. This prevents the difference between the start time
97 * and current time from growing too large, while maintaining the
98 * correct number of samples to render.
100 if(done >= mDevice->Frequency)
102 seconds s{done/mDevice->Frequency};
103 start += s;
104 done -= mDevice->Frequency*s.count();
108 return 0;
112 void NullBackend::open(const ALCchar *name)
114 if(!name)
115 name = nullDevice;
116 else if(strcmp(name, nullDevice) != 0)
117 throw al::backend_exception{ALC_INVALID_VALUE, "Device name \"%s\" not found", name};
119 mDevice->DeviceName = name;
122 bool NullBackend::reset()
124 SetDefaultWFXChannelOrder(mDevice);
125 return true;
128 bool NullBackend::start()
130 try {
131 mKillNow.store(false, std::memory_order_release);
132 mThread = std::thread{std::mem_fn(&NullBackend::mixerProc), this};
133 return true;
135 catch(std::exception& e) {
136 ERR("Failed to start mixing thread: %s\n", e.what());
138 catch(...) {
140 return false;
143 void NullBackend::stop()
145 if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
146 return;
147 mThread.join();
150 } // namespace
153 bool NullBackendFactory::init()
154 { return true; }
156 bool NullBackendFactory::querySupport(BackendType type)
157 { return (type == BackendType::Playback); }
159 void NullBackendFactory::probe(DevProbe type, std::string *outnames)
161 switch(type)
163 case DevProbe::Playback:
164 /* Includes null char. */
165 outnames->append(nullDevice, sizeof(nullDevice));
166 break;
167 case DevProbe::Capture:
168 break;
172 BackendPtr NullBackendFactory::createBackend(ALCdevice *device, BackendType type)
174 if(type == BackendType::Playback)
175 return BackendPtr{new NullBackend{device}};
176 return nullptr;
179 BackendFactory &NullBackendFactory::getFactory()
181 static NullBackendFactory factory{};
182 return factory;