null.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 "almalloc.h"
  30. #include "alstring.h"
  31. #include "althrd_setname.h"
  32. #include "core/device.h"
  33. #include "core/helpers.h"
  34. namespace {
  35. using std::chrono::seconds;
  36. using std::chrono::milliseconds;
  37. using std::chrono::nanoseconds;
  38. using namespace std::string_view_literals;
  39. [[nodiscard]] constexpr auto GetDeviceName() noexcept { return "No Output"sv; }
  40. struct NullBackend final : public BackendBase {
  41. NullBackend(DeviceBase *device) noexcept : BackendBase{device} { }
  42. int mixerProc();
  43. void open(std::string_view name) override;
  44. bool reset() override;
  45. void start() override;
  46. void stop() override;
  47. std::atomic<bool> mKillNow{true};
  48. std::thread mThread;
  49. };
  50. int NullBackend::mixerProc()
  51. {
  52. const milliseconds restTime{mDevice->UpdateSize*1000/mDevice->Frequency / 2};
  53. SetRTPriority();
  54. althrd_setname(GetMixerThreadName());
  55. int64_t done{0};
  56. auto start = std::chrono::steady_clock::now();
  57. while(!mKillNow.load(std::memory_order_acquire)
  58. && mDevice->Connected.load(std::memory_order_acquire))
  59. {
  60. auto now = std::chrono::steady_clock::now();
  61. /* This converts from nanoseconds to nanosamples, then to samples. */
  62. int64_t avail{std::chrono::duration_cast<seconds>((now-start) * mDevice->Frequency).count()};
  63. if(avail-done < mDevice->UpdateSize)
  64. {
  65. std::this_thread::sleep_for(restTime);
  66. continue;
  67. }
  68. while(avail-done >= mDevice->UpdateSize)
  69. {
  70. mDevice->renderSamples(nullptr, mDevice->UpdateSize, 0u);
  71. done += mDevice->UpdateSize;
  72. }
  73. /* For every completed second, increment the start time and reduce the
  74. * samples done. This prevents the difference between the start time
  75. * and current time from growing too large, while maintaining the
  76. * correct number of samples to render.
  77. */
  78. if(done >= mDevice->Frequency)
  79. {
  80. seconds s{done/mDevice->Frequency};
  81. start += s;
  82. done -= mDevice->Frequency*s.count();
  83. }
  84. }
  85. return 0;
  86. }
  87. void NullBackend::open(std::string_view name)
  88. {
  89. if(name.empty())
  90. name = GetDeviceName();
  91. else if(name != GetDeviceName())
  92. throw al::backend_exception{al::backend_error::NoDevice, "Device name \"%.*s\" not found",
  93. al::sizei(name), name.data()};
  94. mDevice->DeviceName = name;
  95. }
  96. bool NullBackend::reset()
  97. {
  98. setDefaultWFXChannelOrder();
  99. return true;
  100. }
  101. void NullBackend::start()
  102. {
  103. try {
  104. mKillNow.store(false, std::memory_order_release);
  105. mThread = std::thread{std::mem_fn(&NullBackend::mixerProc), this};
  106. }
  107. catch(std::exception& e) {
  108. throw al::backend_exception{al::backend_error::DeviceError,
  109. "Failed to start mixing thread: %s", e.what()};
  110. }
  111. }
  112. void NullBackend::stop()
  113. {
  114. if(mKillNow.exchange(true, std::memory_order_acq_rel) || !mThread.joinable())
  115. return;
  116. mThread.join();
  117. }
  118. } // namespace
  119. bool NullBackendFactory::init()
  120. { return true; }
  121. bool NullBackendFactory::querySupport(BackendType type)
  122. { return (type == BackendType::Playback); }
  123. auto NullBackendFactory::enumerate(BackendType type) -> std::vector<std::string>
  124. {
  125. switch(type)
  126. {
  127. case BackendType::Playback:
  128. /* Include null char. */
  129. return std::vector{std::string{GetDeviceName()}};
  130. case BackendType::Capture:
  131. break;
  132. }
  133. return {};
  134. }
  135. BackendPtr NullBackendFactory::createBackend(DeviceBase *device, BackendType type)
  136. {
  137. if(type == BackendType::Playback)
  138. return BackendPtr{new NullBackend{device}};
  139. return nullptr;
  140. }
  141. BackendFactory &NullBackendFactory::getFactory()
  142. {
  143. static NullBackendFactory factory{};
  144. return factory;
  145. }