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 "core/device.h"
35 #include "core/helpers.h"
41 using std::chrono::seconds
;
42 using std::chrono::milliseconds
;
43 using std::chrono::nanoseconds
;
45 constexpr char nullDevice
[] = "No Output";
48 struct NullBackend final
: public BackendBase
{
49 NullBackend(DeviceBase
*device
) noexcept
: BackendBase
{device
} { }
53 void open(const char *name
) override
;
54 bool reset() override
;
55 void start() override
;
58 std::atomic
<bool> mKillNow
{true};
61 DEF_NEWDEL(NullBackend
)
64 int NullBackend::mixerProc()
66 const milliseconds restTime
{mDevice
->UpdateSize
*1000/mDevice
->Frequency
/ 2};
69 althrd_setname(MIXER_THREAD_NAME
);
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(const char *name
)
112 else if(strcmp(name
, nullDevice
) != 0)
113 throw al::backend_exception
{al::backend_error::NoDevice
, "Device name \"%s\" not found",
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 std::string
NullBackendFactory::probe(BackendType type
)
155 std::string outnames
;
158 case BackendType::Playback
:
159 /* Includes null char. */
160 outnames
.append(nullDevice
, sizeof(nullDevice
));
162 case BackendType::Capture
:
168 BackendPtr
NullBackendFactory::createBackend(DeviceBase
*device
, BackendType type
)
170 if(type
== BackendType::Playback
)
171 return BackendPtr
{new NullBackend
{device
}};
175 BackendFactory
&NullBackendFactory::getFactory()
177 static NullBackendFactory factory
{};