2010-05-28 16:41:52 -07:00
|
|
|
/**
|
|
|
|
* OpenAL cross platform audio library
|
|
|
|
* Copyright (C) 2010 by Chris Robinson
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2014-08-18 14:11:03 +02:00
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2010-05-28 16:41:52 -07:00
|
|
|
* Or go to http://www.gnu.org/copyleft/lgpl.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2018-11-15 19:15:14 -08:00
|
|
|
#include "backends/null.h"
|
|
|
|
|
2019-07-28 11:28:36 -07:00
|
|
|
#include <exception>
|
|
|
|
#include <atomic>
|
2018-11-10 19:31:23 -08:00
|
|
|
#include <chrono>
|
2019-07-28 11:28:36 -07:00
|
|
|
#include <cstdint>
|
|
|
|
#include <cstring>
|
2018-12-29 03:11:06 -08:00
|
|
|
#include <functional>
|
2019-07-28 11:28:36 -07:00
|
|
|
#include <thread>
|
2018-11-10 19:31:23 -08:00
|
|
|
|
2019-07-28 18:33:29 -07:00
|
|
|
#include "alcmain.h"
|
2019-10-07 21:37:56 -07:00
|
|
|
#include "alexcpt.h"
|
2019-07-28 11:28:36 -07:00
|
|
|
#include "almalloc.h"
|
2012-09-14 02:14:29 -07:00
|
|
|
#include "alu.h"
|
2019-07-28 11:28:36 -07:00
|
|
|
#include "logging.h"
|
|
|
|
#include "threads.h"
|
2010-05-28 16:41:52 -07:00
|
|
|
|
2013-10-28 08:29:19 -07:00
|
|
|
|
2018-11-09 03:46:30 -08:00
|
|
|
namespace {
|
2010-05-28 16:41:52 -07:00
|
|
|
|
2018-11-10 19:31:23 -08:00
|
|
|
using std::chrono::seconds;
|
|
|
|
using std::chrono::milliseconds;
|
|
|
|
using std::chrono::nanoseconds;
|
|
|
|
|
2018-11-09 03:46:30 -08:00
|
|
|
constexpr ALCchar nullDevice[] = "No Output";
|
|
|
|
|
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
struct NullBackend final : public BackendBase {
|
|
|
|
NullBackend(ALCdevice *device) noexcept : BackendBase{device} { }
|
2018-12-27 21:50:54 -08:00
|
|
|
|
|
|
|
int mixerProc();
|
|
|
|
|
2019-10-07 21:37:56 -07:00
|
|
|
void open(const ALCchar *name) override;
|
2019-09-15 09:50:28 -07:00
|
|
|
bool reset() override;
|
|
|
|
bool start() override;
|
2018-12-28 22:56:20 -08:00
|
|
|
void stop() override;
|
|
|
|
|
2018-12-30 21:58:14 -08:00
|
|
|
std::atomic<bool> mKillNow{true};
|
2018-11-26 17:31:04 -08:00
|
|
|
std::thread mThread;
|
2018-11-09 03:46:30 -08:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
DEF_NEWDEL(NullBackend)
|
|
|
|
};
|
2013-10-29 11:22:18 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
int NullBackend::mixerProc()
|
2010-05-28 16:41:52 -07:00
|
|
|
{
|
2018-12-27 21:50:54 -08:00
|
|
|
const milliseconds restTime{mDevice->UpdateSize*1000/mDevice->Frequency / 2};
|
2010-05-28 16:41:52 -07:00
|
|
|
|
2013-10-27 07:00:44 -07:00
|
|
|
SetRTPriority();
|
2018-11-17 06:07:04 -08:00
|
|
|
althrd_setname(MIXER_THREAD_NAME);
|
2013-10-27 07:00:44 -07:00
|
|
|
|
2019-02-11 12:16:58 -08:00
|
|
|
int64_t done{0};
|
2018-11-10 19:31:23 -08:00
|
|
|
auto start = std::chrono::steady_clock::now();
|
2018-12-27 21:50:54 -08:00
|
|
|
while(!mKillNow.load(std::memory_order_acquire) &&
|
|
|
|
mDevice->Connected.load(std::memory_order_acquire))
|
2010-05-28 16:41:52 -07:00
|
|
|
{
|
2018-11-10 19:31:23 -08:00
|
|
|
auto now = std::chrono::steady_clock::now();
|
2010-05-28 16:41:52 -07:00
|
|
|
|
2018-11-10 19:31:23 -08:00
|
|
|
/* This converts from nanoseconds to nanosamples, then to samples. */
|
2019-02-11 12:16:58 -08:00
|
|
|
int64_t avail{std::chrono::duration_cast<seconds>((now-start) * mDevice->Frequency).count()};
|
2018-12-27 21:50:54 -08:00
|
|
|
if(avail-done < mDevice->UpdateSize)
|
2010-05-28 16:41:52 -07:00
|
|
|
{
|
2018-11-10 19:31:23 -08:00
|
|
|
std::this_thread::sleep_for(restTime);
|
|
|
|
continue;
|
2010-08-02 20:04:52 -07:00
|
|
|
}
|
2018-12-27 21:50:54 -08:00
|
|
|
while(avail-done >= mDevice->UpdateSize)
|
2014-04-17 09:03:57 -07:00
|
|
|
{
|
2019-10-07 23:22:06 -07:00
|
|
|
std::lock_guard<NullBackend> _{*this};
|
2019-12-21 20:43:46 -08:00
|
|
|
aluMixData(mDevice, nullptr, mDevice->UpdateSize, 0u);
|
2018-12-27 21:50:54 -08:00
|
|
|
done += mDevice->UpdateSize;
|
2014-04-17 09:03:57 -07:00
|
|
|
}
|
2018-11-10 19:31:23 -08:00
|
|
|
|
|
|
|
/* For every completed second, increment the start time and reduce the
|
|
|
|
* samples done. This prevents the difference between the start time
|
|
|
|
* and current time from growing too large, while maintaining the
|
|
|
|
* correct number of samples to render.
|
|
|
|
*/
|
2018-12-27 21:50:54 -08:00
|
|
|
if(done >= mDevice->Frequency)
|
2018-11-10 19:31:23 -08:00
|
|
|
{
|
2018-12-27 21:50:54 -08:00
|
|
|
seconds s{done/mDevice->Frequency};
|
2018-11-10 19:31:23 -08:00
|
|
|
start += s;
|
2018-12-27 21:50:54 -08:00
|
|
|
done -= mDevice->Frequency*s.count();
|
2018-11-10 19:31:23 -08:00
|
|
|
}
|
2010-05-28 16:41:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-10-28 08:29:19 -07:00
|
|
|
|
2019-10-07 21:37:56 -07:00
|
|
|
void NullBackend::open(const ALCchar *name)
|
2013-10-28 08:29:19 -07:00
|
|
|
{
|
|
|
|
if(!name)
|
|
|
|
name = nullDevice;
|
|
|
|
else if(strcmp(name, nullDevice) != 0)
|
2019-10-07 21:37:56 -07:00
|
|
|
throw al::backend_exception{ALC_INVALID_VALUE, "Device name \"%s\" not found", name};
|
2010-05-28 16:41:52 -07:00
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
mDevice->DeviceName = name;
|
2010-05-28 16:41:52 -07:00
|
|
|
}
|
|
|
|
|
2019-09-15 09:50:28 -07:00
|
|
|
bool NullBackend::reset()
|
2013-10-28 08:29:19 -07:00
|
|
|
{
|
2018-12-28 22:56:20 -08:00
|
|
|
SetDefaultWFXChannelOrder(mDevice);
|
2019-09-15 09:50:28 -07:00
|
|
|
return true;
|
2010-05-28 16:41:52 -07:00
|
|
|
}
|
|
|
|
|
2019-09-15 09:50:28 -07:00
|
|
|
bool NullBackend::start()
|
2010-05-28 16:41:52 -07:00
|
|
|
{
|
2018-11-11 00:33:04 -08:00
|
|
|
try {
|
2018-12-30 21:58:14 -08:00
|
|
|
mKillNow.store(false, std::memory_order_release);
|
2018-12-28 22:56:20 -08:00
|
|
|
mThread = std::thread{std::mem_fn(&NullBackend::mixerProc), this};
|
2019-09-15 09:50:28 -07:00
|
|
|
return true;
|
2018-11-11 00:33:04 -08:00
|
|
|
}
|
|
|
|
catch(std::exception& e) {
|
|
|
|
ERR("Failed to start mixing thread: %s\n", e.what());
|
|
|
|
}
|
|
|
|
catch(...) {
|
|
|
|
}
|
2019-09-15 09:50:28 -07:00
|
|
|
return false;
|
2012-03-05 07:11:09 -08:00
|
|
|
}
|
|
|
|
|
2018-12-28 22:56:20 -08:00
|
|
|
void NullBackend::stop()
|
2012-03-05 07:11:09 -08:00
|
|
|
{
|
2018-12-30 21:58:14 -08:00
|
|
|
if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
|
2013-10-28 08:29:19 -07:00
|
|
|
return;
|
2018-12-28 22:56:20 -08:00
|
|
|
mThread.join();
|
2010-05-28 16:41:52 -07:00
|
|
|
}
|
|
|
|
|
2018-11-15 19:15:14 -08:00
|
|
|
} // namespace
|
2010-05-28 16:41:52 -07:00
|
|
|
|
2013-11-02 16:35:05 -07:00
|
|
|
|
2018-11-15 19:15:14 -08:00
|
|
|
bool NullBackendFactory::init()
|
2018-11-15 23:58:34 -08:00
|
|
|
{ return true; }
|
2010-05-28 16:41:52 -07:00
|
|
|
|
2018-12-29 01:38:26 -08:00
|
|
|
bool NullBackendFactory::querySupport(BackendType type)
|
|
|
|
{ return (type == BackendType::Playback); }
|
2013-10-28 08:29:19 -07:00
|
|
|
|
2018-12-24 19:29:01 -08:00
|
|
|
void NullBackendFactory::probe(DevProbe type, std::string *outnames)
|
2010-05-28 16:41:52 -07:00
|
|
|
{
|
2011-06-14 04:02:58 -07:00
|
|
|
switch(type)
|
|
|
|
{
|
2019-03-19 00:24:54 -07:00
|
|
|
case DevProbe::Playback:
|
2018-11-15 04:24:33 -08:00
|
|
|
/* Includes null char. */
|
|
|
|
outnames->append(nullDevice, sizeof(nullDevice));
|
|
|
|
break;
|
2019-03-19 00:24:54 -07:00
|
|
|
case DevProbe::Capture:
|
2011-06-14 04:02:58 -07:00
|
|
|
break;
|
|
|
|
}
|
2010-05-28 16:41:52 -07:00
|
|
|
}
|
2013-10-28 08:29:19 -07:00
|
|
|
|
2018-12-29 02:16:16 -08:00
|
|
|
BackendPtr NullBackendFactory::createBackend(ALCdevice *device, BackendType type)
|
2013-10-28 08:29:19 -07:00
|
|
|
{
|
2018-12-29 01:38:26 -08:00
|
|
|
if(type == BackendType::Playback)
|
2018-12-29 02:16:16 -08:00
|
|
|
return BackendPtr{new NullBackend{device}};
|
2018-12-28 22:56:20 -08:00
|
|
|
return nullptr;
|
2013-10-28 08:29:19 -07:00
|
|
|
}
|
2018-11-15 19:15:14 -08:00
|
|
|
|
|
|
|
BackendFactory &NullBackendFactory::getFactory()
|
|
|
|
{
|
|
|
|
static NullBackendFactory factory{};
|
|
|
|
return factory;
|
|
|
|
}
|