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
33 #include "althrd_setname.h"
35 #include "core/device.h"
36 #include "core/helpers.h"
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
} { }
54 void open(std::string_view name
) override
;
55 bool reset() override
;
56 void start() override
;
59 std::atomic
<bool> mKillNow
{true};
63 int NullBackend::mixerProc()
65 const milliseconds restTime
{mDevice
->UpdateSize
*1000/mDevice
->Frequency
/ 2};
68 althrd_setname(MIXER_THREAD_NAME
);
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
);
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
};
99 done
-= mDevice
->Frequency
*s
.count();
107 void NullBackend::open(std::string_view name
)
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();
124 void NullBackend::start()
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())
146 bool NullBackendFactory::init()
149 bool NullBackendFactory::querySupport(BackendType type
)
150 { return (type
== BackendType::Playback
); }
152 std::string
NullBackendFactory::probe(BackendType type
)
156 case BackendType::Playback
:
157 /* Include null char. */
158 return std::string
{GetDeviceName()} + '\0';
159 case BackendType::Capture
:
162 return std::string
{};
165 BackendPtr
NullBackendFactory::createBackend(DeviceBase
*device
, BackendType type
)
167 if(type
== BackendType::Playback
)
168 return BackendPtr
{new NullBackend
{device
}};
172 BackendFactory
&NullBackendFactory::getFactory()
174 static NullBackendFactory factory
{};