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
32 #include "althrd_setname.h"
33 #include "core/device.h"
34 #include "core/helpers.h"
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
} { }
52 void open(std::string_view name
) override
;
53 bool reset() override
;
54 void start() override
;
57 std::atomic
<bool> mKillNow
{true};
61 int NullBackend::mixerProc()
63 const milliseconds restTime
{mDevice
->UpdateSize
*1000/mDevice
->Frequency
/ 2};
66 althrd_setname(GetMixerThreadName());
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
);
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
};
97 done
-= mDevice
->Frequency
*s
.count();
105 void NullBackend::open(std::string_view name
)
108 name
= GetDeviceName();
109 else if(name
!= GetDeviceName())
110 throw al::backend_exception
{al::backend_error::NoDevice
, "Device name \"{}\" not found",
116 bool NullBackend::reset()
118 setDefaultWFXChannelOrder();
122 void NullBackend::start()
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())
144 bool NullBackendFactory::init()
147 bool NullBackendFactory::querySupport(BackendType type
)
148 { return (type
== BackendType::Playback
); }
150 auto NullBackendFactory::enumerate(BackendType type
) -> std::vector
<std::string
>
154 case BackendType::Playback
:
155 /* Include null char. */
156 return std::vector
{std::string
{GetDeviceName()}};
157 case BackendType::Capture
:
163 BackendPtr
NullBackendFactory::createBackend(DeviceBase
*device
, BackendType type
)
165 if(type
== BackendType::Playback
)
166 return BackendPtr
{new NullBackend
{device
}};
170 BackendFactory
&NullBackendFactory::getFactory()
172 static NullBackendFactory factory
{};