null.cpp 4.7 KB

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