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
23 #include "backends/null.h"
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
} { }
55 void open(const ALCchar
*name
) override
;
56 bool reset() override
;
57 void start() override
;
60 std::atomic
<bool> mKillNow
{true};
63 DEF_NEWDEL(NullBackend
)
66 int NullBackend::mixerProc()
68 const milliseconds restTime
{mDevice
->UpdateSize
*1000/mDevice
->Frequency
/ 2};
71 althrd_setname(MIXER_THREAD_NAME
);
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
);
87 while(avail
-done
>= mDevice
->UpdateSize
)
89 mDevice
->renderSamples(nullptr, mDevice
->UpdateSize
, 0u);
90 done
+= mDevice
->UpdateSize
;
93 /* For every completed second, increment the start time and reduce the
94 * samples done. This prevents the difference between the start time
95 * and current time from growing too large, while maintaining the
96 * correct number of samples to render.
98 if(done
>= mDevice
->Frequency
)
100 seconds s
{done
/mDevice
->Frequency
};
102 done
-= mDevice
->Frequency
*s
.count();
110 void NullBackend::open(const ALCchar
*name
)
114 else if(strcmp(name
, nullDevice
) != 0)
115 throw al::backend_exception
{ALC_INVALID_VALUE
, "Device name \"%s\" not found", name
};
117 mDevice
->DeviceName
= name
;
120 bool NullBackend::reset()
122 setDefaultWFXChannelOrder();
126 void NullBackend::start()
129 mKillNow
.store(false, std::memory_order_release
);
130 mThread
= std::thread
{std::mem_fn(&NullBackend::mixerProc
), this};
132 catch(std::exception
& e
) {
133 throw al::backend_exception
{ALC_INVALID_DEVICE
, "Failed to start mixing thread: %s",
138 void NullBackend::stop()
140 if(mKillNow
.exchange(true, std::memory_order_acq_rel
) || !mThread
.joinable())
148 bool NullBackendFactory::init()
151 bool NullBackendFactory::querySupport(BackendType type
)
152 { return (type
== BackendType::Playback
); }
154 std::string
NullBackendFactory::probe(BackendType type
)
156 std::string outnames
;
159 case BackendType::Playback
:
160 /* Includes null char. */
161 outnames
.append(nullDevice
, sizeof(nullDevice
));
163 case BackendType::Capture
:
169 BackendPtr
NullBackendFactory::createBackend(ALCdevice
*device
, BackendType type
)
171 if(type
== BackendType::Playback
)
172 return BackendPtr
{new NullBackend
{device
}};
176 BackendFactory
&NullBackendFactory::getFactory()
178 static NullBackendFactory factory
{};