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 bool 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
)
90 aluMixData(mDevice
, nullptr, mDevice
->UpdateSize
);
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
};
104 done
-= mDevice
->Frequency
*s
.count();
112 void NullBackend::open(const ALCchar
*name
)
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
);
128 bool NullBackend::start()
131 mKillNow
.store(false, std::memory_order_release
);
132 mThread
= std::thread
{std::mem_fn(&NullBackend::mixerProc
), this};
135 catch(std::exception
& e
) {
136 ERR("Failed to start mixing thread: %s\n", e
.what());
143 void NullBackend::stop()
145 if(mKillNow
.exchange(true, std::memory_order_acq_rel
) || !mThread
.joinable())
153 bool NullBackendFactory::init()
156 bool NullBackendFactory::querySupport(BackendType type
)
157 { return (type
== BackendType::Playback
); }
159 void NullBackendFactory::probe(DevProbe type
, std::string
*outnames
)
163 case DevProbe::Playback
:
164 /* Includes null char. */
165 outnames
->append(nullDevice
, sizeof(nullDevice
));
167 case DevProbe::Capture
:
172 BackendPtr
NullBackendFactory::createBackend(ALCdevice
*device
, BackendType type
)
174 if(type
== BackendType::Playback
)
175 return BackendPtr
{new NullBackend
{device
}};
179 BackendFactory
&NullBackendFactory::getFactory()
181 static NullBackendFactory factory
{};