base.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #ifndef ALC_BACKENDS_BASE_H
  2. #define ALC_BACKENDS_BASE_H
  3. #include <chrono>
  4. #include <cstdarg>
  5. #include <cstddef>
  6. #include <memory>
  7. #include <ratio>
  8. #include <string>
  9. #include <string_view>
  10. #include "core/device.h"
  11. #include "core/except.h"
  12. #include "alc/events.h"
  13. using uint = unsigned int;
  14. struct ClockLatency {
  15. std::chrono::nanoseconds ClockTime;
  16. std::chrono::nanoseconds Latency;
  17. };
  18. struct BackendBase {
  19. virtual void open(std::string_view name) = 0;
  20. virtual bool reset();
  21. virtual void start() = 0;
  22. virtual void stop() = 0;
  23. virtual void captureSamples(std::byte *buffer, uint samples);
  24. virtual uint availableSamples();
  25. virtual ClockLatency getClockLatency();
  26. DeviceBase *const mDevice;
  27. BackendBase(DeviceBase *device) noexcept : mDevice{device} { }
  28. virtual ~BackendBase() = default;
  29. protected:
  30. /** Sets the default channel order used by most non-WaveFormatEx-based APIs. */
  31. void setDefaultChannelOrder() const;
  32. /** Sets the default channel order used by WaveFormatEx. */
  33. void setDefaultWFXChannelOrder() const;
  34. };
  35. using BackendPtr = std::unique_ptr<BackendBase>;
  36. enum class BackendType {
  37. Playback,
  38. Capture
  39. };
  40. /* Helper to get the device latency from the backend, including any fixed
  41. * latency from post-processing.
  42. */
  43. inline ClockLatency GetClockLatency(DeviceBase *device, BackendBase *backend)
  44. {
  45. ClockLatency ret{backend->getClockLatency()};
  46. ret.Latency += device->FixedLatency;
  47. return ret;
  48. }
  49. struct BackendFactory {
  50. virtual ~BackendFactory() = default;
  51. virtual bool init() = 0;
  52. virtual bool querySupport(BackendType type) = 0;
  53. virtual alc::EventSupport queryEventSupport(alc::EventType, BackendType)
  54. { return alc::EventSupport::NoSupport; }
  55. virtual std::string probe(BackendType type) = 0;
  56. virtual BackendPtr createBackend(DeviceBase *device, BackendType type) = 0;
  57. };
  58. namespace al {
  59. enum class backend_error {
  60. NoDevice,
  61. DeviceError,
  62. OutOfMemory
  63. };
  64. class backend_exception final : public base_exception {
  65. backend_error mErrorCode;
  66. public:
  67. #ifdef __MINGW32__
  68. [[gnu::format(__MINGW_PRINTF_FORMAT, 3, 4)]]
  69. #else
  70. [[gnu::format(printf, 3, 4)]]
  71. #endif
  72. backend_exception(backend_error code, const char *msg, ...);
  73. ~backend_exception() override;
  74. [[nodiscard]] auto errorCode() const noexcept -> backend_error { return mErrorCode; }
  75. };
  76. } // namespace al
  77. #endif /* ALC_BACKENDS_BASE_H */