Free Electron
base.h
1 #ifndef AL_BACKENDS_BASE_H
2 #define AL_BACKENDS_BASE_H
3 
4 #include "alMain.h"
5 #include "threads.h"
6 #include "alstring.h"
7 
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 typedef struct ClockLatency {
14  ALint64 ClockTime;
15  ALint64 Latency;
16 } ClockLatency;
17 
18 /* Helper to get the current clock time from the device's ClockBase, and
19  * SamplesDone converted from the sample rate.
20  */
21 inline ALuint64 GetDeviceClockTime(ALCdevice *device)
22 {
23  return device->ClockBase + (device->SamplesDone * DEVICE_CLOCK_RES /
24  device->Frequency);
25 }
26 
27 
28 struct ALCbackendVtable;
29 
30 typedef struct ALCbackend {
31  const struct ALCbackendVtable *vtbl;
32 
33  ALCdevice *mDevice;
34 
35  almtx_t mMutex;
36 } ALCbackend;
37 
38 void ALCbackend_Construct(ALCbackend *self, ALCdevice *device);
39 void ALCbackend_Destruct(ALCbackend *self);
40 ALCboolean ALCbackend_reset(ALCbackend *self);
41 ALCenum ALCbackend_captureSamples(ALCbackend *self, void *buffer, ALCuint samples);
42 ALCuint ALCbackend_availableSamples(ALCbackend *self);
43 ClockLatency ALCbackend_getClockLatency(ALCbackend *self);
44 void ALCbackend_lock(ALCbackend *self);
45 void ALCbackend_unlock(ALCbackend *self);
46 
47 struct ALCbackendVtable {
48  void (*const Destruct)(ALCbackend*);
49 
50  ALCenum (*const open)(ALCbackend*, const ALCchar*);
51 
52  ALCboolean (*const reset)(ALCbackend*);
53  ALCboolean (*const start)(ALCbackend*);
54  void (*const stop)(ALCbackend*);
55 
56  ALCenum (*const captureSamples)(ALCbackend*, void*, ALCuint);
57  ALCuint (*const availableSamples)(ALCbackend*);
58 
59  ClockLatency (*const getClockLatency)(ALCbackend*);
60 
61  void (*const lock)(ALCbackend*);
62  void (*const unlock)(ALCbackend*);
63 
64  void (*const Delete)(void*);
65 };
66 
67 #define DEFINE_ALCBACKEND_VTABLE(T) \
68 DECLARE_THUNK(T, ALCbackend, void, Destruct) \
69 DECLARE_THUNK1(T, ALCbackend, ALCenum, open, const ALCchar*) \
70 DECLARE_THUNK(T, ALCbackend, ALCboolean, reset) \
71 DECLARE_THUNK(T, ALCbackend, ALCboolean, start) \
72 DECLARE_THUNK(T, ALCbackend, void, stop) \
73 DECLARE_THUNK2(T, ALCbackend, ALCenum, captureSamples, void*, ALCuint) \
74 DECLARE_THUNK(T, ALCbackend, ALCuint, availableSamples) \
75 DECLARE_THUNK(T, ALCbackend, ClockLatency, getClockLatency) \
76 DECLARE_THUNK(T, ALCbackend, void, lock) \
77 DECLARE_THUNK(T, ALCbackend, void, unlock) \
78 static void T##_ALCbackend_Delete(void *ptr) \
79 { T##_Delete(STATIC_UPCAST(T, ALCbackend, (ALCbackend*)ptr)); } \
80  \
81 static const struct ALCbackendVtable T##_ALCbackend_vtable = { \
82  T##_ALCbackend_Destruct, \
83  \
84  T##_ALCbackend_open, \
85  T##_ALCbackend_reset, \
86  T##_ALCbackend_start, \
87  T##_ALCbackend_stop, \
88  T##_ALCbackend_captureSamples, \
89  T##_ALCbackend_availableSamples, \
90  T##_ALCbackend_getClockLatency, \
91  T##_ALCbackend_lock, \
92  T##_ALCbackend_unlock, \
93  \
94  T##_ALCbackend_Delete, \
95 }
96 
97 
98 typedef enum ALCbackend_Type {
99  ALCbackend_Playback,
100  ALCbackend_Capture,
101  ALCbackend_Loopback
102 } ALCbackend_Type;
103 
104 
105 struct ALCbackendFactoryVtable;
106 
107 typedef struct ALCbackendFactory {
108  const struct ALCbackendFactoryVtable *vtbl;
109 } ALCbackendFactory;
110 
111 void ALCbackendFactory_deinit(ALCbackendFactory *self);
112 
113 struct ALCbackendFactoryVtable {
114  ALCboolean (*const init)(ALCbackendFactory *self);
115  void (*const deinit)(ALCbackendFactory *self);
116 
117  ALCboolean (*const querySupport)(ALCbackendFactory *self, ALCbackend_Type type);
118 
119  void (*const probe)(ALCbackendFactory *self, enum DevProbe type, al_string *outnames);
120 
121  ALCbackend* (*const createBackend)(ALCbackendFactory *self, ALCdevice *device, ALCbackend_Type type);
122 };
123 
124 #define DEFINE_ALCBACKENDFACTORY_VTABLE(T) \
125 DECLARE_THUNK(T, ALCbackendFactory, ALCboolean, init) \
126 DECLARE_THUNK(T, ALCbackendFactory, void, deinit) \
127 DECLARE_THUNK1(T, ALCbackendFactory, ALCboolean, querySupport, ALCbackend_Type) \
128 DECLARE_THUNK2(T, ALCbackendFactory, void, probe, enum DevProbe, al_string*) \
129 DECLARE_THUNK2(T, ALCbackendFactory, ALCbackend*, createBackend, ALCdevice*, ALCbackend_Type) \
130  \
131 static const struct ALCbackendFactoryVtable T##_ALCbackendFactory_vtable = { \
132  T##_ALCbackendFactory_init, \
133  T##_ALCbackendFactory_deinit, \
134  T##_ALCbackendFactory_querySupport, \
135  T##_ALCbackendFactory_probe, \
136  T##_ALCbackendFactory_createBackend, \
137 }
138 
139 
140 ALCbackendFactory *ALCpulseBackendFactory_getFactory(void);
141 ALCbackendFactory *ALCalsaBackendFactory_getFactory(void);
142 ALCbackendFactory *ALCcoreAudioBackendFactory_getFactory(void);
143 ALCbackendFactory *ALCossBackendFactory_getFactory(void);
144 ALCbackendFactory *ALCjackBackendFactory_getFactory(void);
145 ALCbackendFactory *ALCsolarisBackendFactory_getFactory(void);
146 ALCbackendFactory *SndioBackendFactory_getFactory(void);
147 ALCbackendFactory *ALCqsaBackendFactory_getFactory(void);
148 ALCbackendFactory *ALCwasapiBackendFactory_getFactory(void);
149 ALCbackendFactory *ALCdsoundBackendFactory_getFactory(void);
150 ALCbackendFactory *ALCwinmmBackendFactory_getFactory(void);
151 ALCbackendFactory *ALCportBackendFactory_getFactory(void);
152 ALCbackendFactory *ALCopenslBackendFactory_getFactory(void);
153 ALCbackendFactory *ALCnullBackendFactory_getFactory(void);
154 ALCbackendFactory *ALCwaveBackendFactory_getFactory(void);
155 ALCbackendFactory *ALCsdl2BackendFactory_getFactory(void);
156 ALCbackendFactory *ALCloopbackFactory_getFactory(void);
157 
158 
159 inline void ALCdevice_Lock(ALCdevice *device)
160 { V0(device->Backend,lock)(); }
161 
162 inline void ALCdevice_Unlock(ALCdevice *device)
163 { V0(device->Backend,unlock)(); }
164 
165 
166 inline ClockLatency GetClockLatency(ALCdevice *device)
167 {
168  ClockLatency ret = V0(device->Backend,getClockLatency)();
169  ret.Latency += device->FixedLatency;
170  return ret;
171 }
172 
173 
174 #ifdef __cplusplus
175 } /* extern "C" */
176 #endif
177 
178 #endif /* AL_BACKENDS_BASE_H */