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
35 #include "althrd_setname.h"
36 #include "core/device.h"
37 #include "core/helpers.h"
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
} { }
55 void open(std::string_view name
) override
;
56 bool reset() override
;
57 void start() override
;
60 std::atomic
<bool> mKillNow
{true};
64 int NullBackend::mixerProc()
66 const milliseconds restTime
{mDevice
->UpdateSize
*1000/mDevice
->Frequency
/ 2};
69 althrd_setname(GetMixerThreadName());
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
);
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
};
100 done
-= mDevice
->Frequency
*s
.count();
108 void NullBackend::open(std::string_view name
)
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();
125 void NullBackend::start()
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())
147 bool NullBackendFactory::init()
150 bool NullBackendFactory::querySupport(BackendType type
)
151 { return (type
== BackendType::Playback
); }
153 auto NullBackendFactory::enumerate(BackendType type
) -> std::vector
<std::string
>
157 case BackendType::Playback
:
158 /* Include null char. */
159 return std::vector
{std::string
{GetDeviceName()}};
160 case BackendType::Capture
:
166 BackendPtr
NullBackendFactory::createBackend(DeviceBase
*device
, BackendType type
)
168 if(type
== BackendType::Playback
)
169 return BackendPtr
{new NullBackend
{device
}};
173 BackendFactory
&NullBackendFactory::getFactory()
175 static NullBackendFactory factory
{};