1 #ifndef ALC_BACKENDS_BASE_H
2 #define ALC_BACKENDS_BASE_H
3
4 #include <chrono>
5 #include <memory>
6 #include <mutex>
7 #include <string>
8
9 #include "albyte.h"
10 #include "alcmain.h"
11 #include "core/except.h"
12
13
14 using uint = unsigned int;
15
16 struct ClockLatency {
17 std::chrono::nanoseconds ClockTime;
18 std::chrono::nanoseconds Latency;
19 };
20
21 struct BackendBase {
22 virtual void open(const char *name) = 0;
23
24 virtual bool reset();
25 virtual void start() = 0;
26 virtual void stop() = 0;
27
28 virtual void captureSamples(al::byte *buffer, uint samples);
29 virtual uint availableSamples();
30
31 virtual ClockLatency getClockLatency();
32
33 ALCdevice *const mDevice;
34
BackendBaseBackendBase35 BackendBase(ALCdevice *device) noexcept : mDevice{device} { }
36 virtual ~BackendBase() = default;
37
38 protected:
39 /** Sets the default channel order used by most non-WaveFormatEx-based APIs. */
40 void setDefaultChannelOrder();
41 /** Sets the default channel order used by WaveFormatEx. */
42 void setDefaultWFXChannelOrder();
43
44 #ifdef _WIN32
45 /** Sets the channel order given the WaveFormatEx mask. */
46 void setChannelOrderFromWFXMask(uint chanmask);
47 #endif
48 };
49 using BackendPtr = std::unique_ptr<BackendBase>;
50
51 enum class BackendType {
52 Playback,
53 Capture
54 };
55
56
57 /* Helper to get the current clock time from the device's ClockBase, and
58 * SamplesDone converted from the sample rate.
59 */
GetDeviceClockTime(ALCdevice * device)60 inline std::chrono::nanoseconds GetDeviceClockTime(ALCdevice *device)
61 {
62 using std::chrono::seconds;
63 using std::chrono::nanoseconds;
64
65 auto ns = nanoseconds{seconds{device->SamplesDone}} / device->Frequency;
66 return device->ClockBase + ns;
67 }
68
69 /* Helper to get the device latency from the backend, including any fixed
70 * latency from post-processing.
71 */
GetClockLatency(ALCdevice * device)72 inline ClockLatency GetClockLatency(ALCdevice *device)
73 {
74 BackendBase *backend{device->Backend.get()};
75 ClockLatency ret{backend->getClockLatency()};
76 ret.Latency += device->FixedLatency;
77 return ret;
78 }
79
80
81 struct BackendFactory {
82 virtual bool init() = 0;
83
84 virtual bool querySupport(BackendType type) = 0;
85
86 virtual std::string probe(BackendType type) = 0;
87
88 virtual BackendPtr createBackend(ALCdevice *device, BackendType type) = 0;
89
90 protected:
91 virtual ~BackendFactory() = default;
92 };
93
94 namespace al {
95
96 enum class backend_error {
97 NoDevice,
98 DeviceError,
99 OutOfMemory
100 };
101
102 class backend_exception final : public base_exception {
103 backend_error mErrorCode;
104
105 public:
106 [[gnu::format(printf, 3, 4)]]
backend_exception(backend_error code,const char * msg,...)107 backend_exception(backend_error code, const char *msg, ...) : mErrorCode{code}
108 {
109 std::va_list args;
110 va_start(args, msg);
111 setMessage(msg, args);
112 va_end(args);
113 }
errorCode()114 backend_error errorCode() const noexcept { return mErrorCode; }
115 };
116
117 } // namespace al
118
119 #endif /* ALC_BACKENDS_BASE_H */
120