🛠️🐜 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.

179 lines
4.6 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 "null.h"
  22. #include <exception>
  23. #include <atomic>
  24. #include <chrono>
  25. #include <cstdint>
  26. #include <cstring>
  27. #include <functional>
  28. #include <thread>
  29. #include "core/device.h"
  30. #include "almalloc.h"
  31. #include "core/helpers.h"
  32. #include "threads.h"
  33. namespace {
  34. using std::chrono::seconds;
  35. using std::chrono::milliseconds;
  36. using std::chrono::nanoseconds;
  37. constexpr char nullDevice[] = "No Output";
  38. struct NullBackend final : public BackendBase {
  39. NullBackend(DeviceBase *device) noexcept : BackendBase{device} { }
  40. int mixerProc();
  41. void open(const char *name) override;
  42. bool reset() override;
  43. void start() override;
  44. void stop() override;
  45. std::atomic<bool> mKillNow{true};
  46. std::thread mThread;
  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. mDevice->renderSamples(nullptr, mDevice->UpdateSize, 0u);
  70. done += mDevice->UpdateSize;
  71. }
  72. /* For every completed second, increment the start time and reduce the
  73. * samples done. This prevents the difference between the start time
  74. * and current time from growing too large, while maintaining the
  75. * correct number of samples to render.
  76. */
  77. if(done >= mDevice->Frequency)
  78. {
  79. seconds s{done/mDevice->Frequency};
  80. start += s;
  81. done -= mDevice->Frequency*s.count();
  82. }
  83. }
  84. return 0;
  85. }
  86. void NullBackend::open(const char *name)
  87. {
  88. if(!name)
  89. name = nullDevice;
  90. else if(strcmp(name, nullDevice) != 0)
  91. throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%s\" not found",
  92. name};
  93. mDevice->DeviceName = name;
  94. }
  95. bool NullBackend::reset()
  96. {
  97. setDefaultWFXChannelOrder();
  98. return true;
  99. }
  100. void NullBackend::start()
  101. {
  102. try {
  103. mKillNow.store(false, std::memory_order_release);
  104. mThread = std::thread{std::mem_fn(&NullBackend::mixerProc), this};
  105. }
  106. catch(std::exception& e) {
  107. throw al::backend_exception{al::backend_error::DeviceError,
  108. "Failed to start mixing thread: %s", e.what()};
  109. }
  110. }
  111. void NullBackend::stop()
  112. {
  113. if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
  114. return;
  115. mThread.join();
  116. }
  117. } // namespace
  118. bool NullBackendFactory::init()
  119. { return true; }
  120. bool NullBackendFactory::querySupport(BackendType type)
  121. { return (type == BackendType::Playback); }
  122. std::string NullBackendFactory::probe(BackendType type)
  123. {
  124. std::string outnames;
  125. switch(type)
  126. {
  127. case BackendType::Playback:
  128. /* Includes null char. */
  129. outnames.append(nullDevice, sizeof(nullDevice));
  130. break;
  131. case BackendType::Capture:
  132. break;
  133. }
  134. return outnames;
  135. }
  136. BackendPtr NullBackendFactory::createBackend(DeviceBase *device, BackendType type)
  137. {
  138. if(type == BackendType::Playback)
  139. return BackendPtr{new NullBackend{device}};
  140. return nullptr;
  141. }
  142. BackendFactory &NullBackendFactory::getFactory()
  143. {
  144. static NullBackendFactory factory{};
  145. return factory;
  146. }