null.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <exception>
  23. #include <atomic>
  24. #include <chrono>
  25. #include <cstdint>
  26. #include <cstring>
  27. #include <functional>
  28. #include <thread>
  29. #include "alcmain.h"
  30. #include "alexcpt.h"
  31. #include "almalloc.h"
  32. #include "alu.h"
  33. #include "logging.h"
  34. #include "threads.h"
  35. namespace {
  36. using std::chrono::seconds;
  37. using std::chrono::milliseconds;
  38. using std::chrono::nanoseconds;
  39. constexpr ALCchar nullDevice[] = "No Output";
  40. struct NullBackend final : public BackendBase {
  41. NullBackend(ALCdevice *device) noexcept : BackendBase{device} { }
  42. int mixerProc();
  43. void open(const ALCchar *name) override;
  44. bool reset() override;
  45. bool start() override;
  46. void stop() override;
  47. std::atomic<bool> mKillNow{true};
  48. std::thread mThread;
  49. DEF_NEWDEL(NullBackend)
  50. };
  51. int NullBackend::mixerProc()
  52. {
  53. const milliseconds restTime{mDevice->UpdateSize*1000/mDevice->Frequency / 2};
  54. SetRTPriority();
  55. althrd_setname(MIXER_THREAD_NAME);
  56. int64_t done{0};
  57. auto start = std::chrono::steady_clock::now();
  58. while(!mKillNow.load(std::memory_order_acquire) &&
  59. mDevice->Connected.load(std::memory_order_acquire))
  60. {
  61. auto now = std::chrono::steady_clock::now();
  62. /* This converts from nanoseconds to nanosamples, then to samples. */
  63. int64_t avail{std::chrono::duration_cast<seconds>((now-start) * mDevice->Frequency).count()};
  64. if(avail-done < mDevice->UpdateSize)
  65. {
  66. std::this_thread::sleep_for(restTime);
  67. continue;
  68. }
  69. while(avail-done >= mDevice->UpdateSize)
  70. {
  71. std::lock_guard<NullBackend> _{*this};
  72. aluMixData(mDevice, nullptr, mDevice->UpdateSize);
  73. done += mDevice->UpdateSize;
  74. }
  75. /* For every completed second, increment the start time and reduce the
  76. * samples done. This prevents the difference between the start time
  77. * and current time from growing too large, while maintaining the
  78. * correct number of samples to render.
  79. */
  80. if(done >= mDevice->Frequency)
  81. {
  82. seconds s{done/mDevice->Frequency};
  83. start += s;
  84. done -= mDevice->Frequency*s.count();
  85. }
  86. }
  87. return 0;
  88. }
  89. void NullBackend::open(const ALCchar *name)
  90. {
  91. if(!name)
  92. name = nullDevice;
  93. else if(strcmp(name, nullDevice) != 0)
  94. throw al::backend_exception{ALC_INVALID_VALUE, "Device name \"%s\" not found", name};
  95. mDevice->DeviceName = name;
  96. }
  97. bool NullBackend::reset()
  98. {
  99. SetDefaultWFXChannelOrder(mDevice);
  100. return true;
  101. }
  102. bool 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 true;
  108. }
  109. catch(std::exception& e) {
  110. ERR("Failed to start mixing thread: %s\n", e.what());
  111. }
  112. catch(...) {
  113. }
  114. return 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. }