🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
4.7 KiB

  1. /**
  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.
  8. *
  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.
  13. *
  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
  19. */
  20. #include "config.h"
  21. #include "backends/null.h"
  22. #include <cstdlib>
  23. #ifdef HAVE_WINDOWS_H
  24. #include <windows.h>
  25. #endif
  26. #include <chrono>
  27. #include <thread>
  28. #include <functional>
  29. #include "alMain.h"
  30. #include "alu.h"
  31. #include "compat.h"
  32. namespace {
  33. using std::chrono::seconds;
  34. using std::chrono::milliseconds;
  35. using std::chrono::nanoseconds;
  36. constexpr ALCchar nullDevice[] = "No Output";
  37. struct NullBackend final : public BackendBase {
  38. NullBackend(ALCdevice *device) noexcept : BackendBase{device} { }
  39. int mixerProc();
  40. ALCenum open(const ALCchar *name) override;
  41. ALCboolean reset() override;
  42. ALCboolean start() override;
  43. void stop() override;
  44. std::atomic<bool> mKillNow{true};
  45. std::thread mThread;
  46. static constexpr inline const char *CurrentPrefix() noexcept { return "NullBackend::"; }
  47. DEF_NEWDEL(NullBackend)
  48. };
  49. int NullBackend::mixerProc()
  50. {
  51. const milliseconds restTime{mDevice->UpdateSize*1000/mDevice->Frequency / 2};
  52. SetRTPriority();
  53. althrd_setname(MIXER_THREAD_NAME);
  54. int64_t done{0};
  55. auto start = std::chrono::steady_clock::now();
  56. while(!mKillNow.load(std::memory_order_acquire) &&
  57. mDevice->Connected.load(std::memory_order_acquire))
  58. {
  59. auto now = std::chrono::steady_clock::now();
  60. /* This converts from nanoseconds to nanosamples, then to samples. */
  61. int64_t avail{std::chrono::duration_cast<seconds>((now-start) * mDevice->Frequency).count()};
  62. if(avail-done < mDevice->UpdateSize)
  63. {
  64. std::this_thread::sleep_for(restTime);
  65. continue;
  66. }
  67. while(avail-done >= mDevice->UpdateSize)
  68. {
  69. lock();
  70. aluMixData(mDevice, nullptr, mDevice->UpdateSize);
  71. unlock();
  72. done += mDevice->UpdateSize;
  73. }
  74. /* For every completed second, increment the start time and reduce the
  75. * samples done. This prevents the difference between the start time
  76. * and current time from growing too large, while maintaining the
  77. * correct number of samples to render.
  78. */
  79. if(done >= mDevice->Frequency)
  80. {
  81. seconds s{done/mDevice->Frequency};
  82. start += s;
  83. done -= mDevice->Frequency*s.count();
  84. }
  85. }
  86. return 0;
  87. }
  88. ALCenum NullBackend::open(const ALCchar *name)
  89. {
  90. if(!name)
  91. name = nullDevice;
  92. else if(strcmp(name, nullDevice) != 0)
  93. return ALC_INVALID_VALUE;
  94. mDevice->DeviceName = name;
  95. return ALC_NO_ERROR;
  96. }
  97. ALCboolean NullBackend::reset()
  98. {
  99. SetDefaultWFXChannelOrder(mDevice);
  100. return ALC_TRUE;
  101. }
  102. ALCboolean NullBackend::start()
  103. {
  104. try {
  105. mKillNow.store(false, std::memory_order_release);
  106. mThread = std::thread{std::mem_fn(&NullBackend::mixerProc), this};
  107. return ALC_TRUE;
  108. }
  109. catch(std::exception& e) {
  110. ERR("Failed to start mixing thread: %s\n", e.what());
  111. }
  112. catch(...) {
  113. }
  114. return ALC_FALSE;
  115. }
  116. void NullBackend::stop()
  117. {
  118. if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
  119. return;
  120. mThread.join();
  121. }
  122. } // namespace
  123. bool NullBackendFactory::init()
  124. { return true; }
  125. bool NullBackendFactory::querySupport(BackendType type)
  126. { return (type == BackendType::Playback); }
  127. void NullBackendFactory::probe(DevProbe type, std::string *outnames)
  128. {
  129. switch(type)
  130. {
  131. case DevProbe::Playback:
  132. /* Includes null char. */
  133. outnames->append(nullDevice, sizeof(nullDevice));
  134. break;
  135. case DevProbe::Capture:
  136. break;
  137. }
  138. }
  139. BackendPtr NullBackendFactory::createBackend(ALCdevice *device, BackendType type)
  140. {
  141. if(type == BackendType::Playback)
  142. return BackendPtr{new NullBackend{device}};
  143. return nullptr;
  144. }
  145. BackendFactory &NullBackendFactory::getFactory()
  146. {
  147. static NullBackendFactory factory{};
  148. return factory;
  149. }