Don't lock the device state when preparing a new effect state
[openal-soft.git] / alc / backends / base.h
blob2503c3df2166e2de7d8e194012ae4a3b34675825
1 #ifndef ALC_BACKENDS_BASE_H
2 #define ALC_BACKENDS_BASE_H
4 #include <chrono>
5 #include <cstdarg>
6 #include <cstddef>
7 #include <memory>
8 #include <ratio>
9 #include <string>
10 #include <string_view>
11 #include <vector>
13 #include "core/device.h"
14 #include "core/except.h"
15 #include "alc/events.h"
18 using uint = unsigned int;
20 struct ClockLatency {
21 std::chrono::nanoseconds ClockTime;
22 std::chrono::nanoseconds Latency;
25 struct BackendBase {
26 virtual void open(std::string_view name) = 0;
28 virtual bool reset();
29 virtual void start() = 0;
30 virtual void stop() = 0;
32 virtual void captureSamples(std::byte *buffer, uint samples);
33 virtual uint availableSamples();
35 virtual ClockLatency getClockLatency();
37 DeviceBase *const mDevice;
39 BackendBase() = delete;
40 BackendBase(const BackendBase&) = delete;
41 BackendBase(BackendBase&&) = delete;
42 BackendBase(DeviceBase *device) noexcept : mDevice{device} { }
43 virtual ~BackendBase() = default;
45 void operator=(const BackendBase&) = delete;
46 void operator=(BackendBase&&) = delete;
48 protected:
49 /** Sets the default channel order used by most non-WaveFormatEx-based APIs. */
50 void setDefaultChannelOrder() const;
51 /** Sets the default channel order used by WaveFormatEx. */
52 void setDefaultWFXChannelOrder() const;
54 using BackendPtr = std::unique_ptr<BackendBase>;
56 enum class BackendType {
57 Playback,
58 Capture
62 /* Helper to get the device latency from the backend, including any fixed
63 * latency from post-processing.
65 inline ClockLatency GetClockLatency(DeviceBase *device, BackendBase *backend)
67 ClockLatency ret{backend->getClockLatency()};
68 ret.Latency += device->FixedLatency;
69 return ret;
73 struct BackendFactory {
74 BackendFactory() = default;
75 BackendFactory(const BackendFactory&) = delete;
76 BackendFactory(BackendFactory&&) = delete;
77 virtual ~BackendFactory() = default;
79 void operator=(const BackendFactory&) = delete;
80 void operator=(BackendFactory&&) = delete;
82 virtual auto init() -> bool = 0;
84 virtual auto querySupport(BackendType type) -> bool = 0;
86 virtual auto queryEventSupport(alc::EventType, BackendType) -> alc::EventSupport
87 { return alc::EventSupport::NoSupport; }
89 virtual auto enumerate(BackendType type) -> std::vector<std::string> = 0;
91 virtual auto createBackend(DeviceBase *device, BackendType type) -> BackendPtr = 0;
94 namespace al {
96 enum class backend_error {
97 NoDevice,
98 DeviceError,
99 OutOfMemory
102 class backend_exception final : public base_exception {
103 backend_error mErrorCode;
105 public:
106 #ifdef __MINGW32__
107 [[gnu::format(__MINGW_PRINTF_FORMAT, 3, 4)]]
108 #else
109 [[gnu::format(printf, 3, 4)]]
110 #endif
111 backend_exception(backend_error code, const char *msg, ...);
112 ~backend_exception() override;
114 [[nodiscard]] auto errorCode() const noexcept -> backend_error { return mErrorCode; }
117 } // namespace al
119 #endif /* ALC_BACKENDS_BASE_H */