base.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "config.h"
  2. #include <stdlib.h>
  3. #include "alMain.h"
  4. #include "backends/base.h"
  5. extern inline ALuint64 GetDeviceClockTime(ALCdevice *device);
  6. /* Base ALCbackend method implementations. */
  7. void ALCbackend_Construct(ALCbackend *self, ALCdevice *device)
  8. {
  9. int ret = almtx_init(&self->mMutex, almtx_recursive);
  10. assert(ret == althrd_success);
  11. self->mDevice = device;
  12. }
  13. void ALCbackend_Destruct(ALCbackend *self)
  14. {
  15. almtx_destroy(&self->mMutex);
  16. }
  17. ALCboolean ALCbackend_reset(ALCbackend* UNUSED(self))
  18. {
  19. return ALC_FALSE;
  20. }
  21. ALCenum ALCbackend_captureSamples(ALCbackend* UNUSED(self), void* UNUSED(buffer), ALCuint UNUSED(samples))
  22. {
  23. return ALC_INVALID_DEVICE;
  24. }
  25. ALCuint ALCbackend_availableSamples(ALCbackend* UNUSED(self))
  26. {
  27. return 0;
  28. }
  29. ClockLatency ALCbackend_getClockLatency(ALCbackend *self)
  30. {
  31. ALCdevice *device = self->mDevice;
  32. ClockLatency ret;
  33. almtx_lock(&self->mMutex);
  34. ret.ClockTime = GetDeviceClockTime(device);
  35. // TODO: Perhaps should be NumUpdates-1 worth of UpdateSize?
  36. ret.Latency = 0;
  37. almtx_unlock(&self->mMutex);
  38. return ret;
  39. }
  40. void ALCbackend_lock(ALCbackend *self)
  41. {
  42. int ret = almtx_lock(&self->mMutex);
  43. assert(ret == althrd_success);
  44. }
  45. void ALCbackend_unlock(ALCbackend *self)
  46. {
  47. int ret = almtx_unlock(&self->mMutex);
  48. assert(ret == althrd_success);
  49. }
  50. /* Base ALCbackendFactory method implementations. */
  51. void ALCbackendFactory_deinit(ALCbackendFactory* UNUSED(self))
  52. {
  53. }
  54. /* Wrappers to use an old-style backend with the new interface. */
  55. typedef struct PlaybackWrapper {
  56. DERIVE_FROM_TYPE(ALCbackend);
  57. const BackendFuncs *Funcs;
  58. } PlaybackWrapper;
  59. static void PlaybackWrapper_Construct(PlaybackWrapper *self, ALCdevice *device, const BackendFuncs *funcs);
  60. static DECLARE_FORWARD(PlaybackWrapper, ALCbackend, void, Destruct)
  61. static ALCenum PlaybackWrapper_open(PlaybackWrapper *self, const ALCchar *name);
  62. static void PlaybackWrapper_close(PlaybackWrapper *self);
  63. static ALCboolean PlaybackWrapper_reset(PlaybackWrapper *self);
  64. static ALCboolean PlaybackWrapper_start(PlaybackWrapper *self);
  65. static void PlaybackWrapper_stop(PlaybackWrapper *self);
  66. static DECLARE_FORWARD2(PlaybackWrapper, ALCbackend, ALCenum, captureSamples, void*, ALCuint)
  67. static DECLARE_FORWARD(PlaybackWrapper, ALCbackend, ALCuint, availableSamples)
  68. static DECLARE_FORWARD(PlaybackWrapper, ALCbackend, ClockLatency, getClockLatency)
  69. static DECLARE_FORWARD(PlaybackWrapper, ALCbackend, void, lock)
  70. static DECLARE_FORWARD(PlaybackWrapper, ALCbackend, void, unlock)
  71. DECLARE_DEFAULT_ALLOCATORS(PlaybackWrapper)
  72. DEFINE_ALCBACKEND_VTABLE(PlaybackWrapper);
  73. static void PlaybackWrapper_Construct(PlaybackWrapper *self, ALCdevice *device, const BackendFuncs *funcs)
  74. {
  75. ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
  76. SET_VTABLE2(PlaybackWrapper, ALCbackend, self);
  77. self->Funcs = funcs;
  78. }
  79. static ALCenum PlaybackWrapper_open(PlaybackWrapper *self, const ALCchar *name)
  80. {
  81. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  82. return self->Funcs->OpenPlayback(device, name);
  83. }
  84. static void PlaybackWrapper_close(PlaybackWrapper *self)
  85. {
  86. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  87. self->Funcs->ClosePlayback(device);
  88. }
  89. static ALCboolean PlaybackWrapper_reset(PlaybackWrapper *self)
  90. {
  91. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  92. return self->Funcs->ResetPlayback(device);
  93. }
  94. static ALCboolean PlaybackWrapper_start(PlaybackWrapper *self)
  95. {
  96. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  97. return self->Funcs->StartPlayback(device);
  98. }
  99. static void PlaybackWrapper_stop(PlaybackWrapper *self)
  100. {
  101. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  102. self->Funcs->StopPlayback(device);
  103. }
  104. typedef struct CaptureWrapper {
  105. DERIVE_FROM_TYPE(ALCbackend);
  106. const BackendFuncs *Funcs;
  107. } CaptureWrapper;
  108. static void CaptureWrapper_Construct(CaptureWrapper *self, ALCdevice *device, const BackendFuncs *funcs);
  109. static DECLARE_FORWARD(CaptureWrapper, ALCbackend, void, Destruct)
  110. static ALCenum CaptureWrapper_open(CaptureWrapper *self, const ALCchar *name);
  111. static void CaptureWrapper_close(CaptureWrapper *self);
  112. static DECLARE_FORWARD(CaptureWrapper, ALCbackend, ALCboolean, reset)
  113. static ALCboolean CaptureWrapper_start(CaptureWrapper *self);
  114. static void CaptureWrapper_stop(CaptureWrapper *self);
  115. static ALCenum CaptureWrapper_captureSamples(CaptureWrapper *self, void *buffer, ALCuint samples);
  116. static ALCuint CaptureWrapper_availableSamples(CaptureWrapper *self);
  117. static DECLARE_FORWARD(CaptureWrapper, ALCbackend, ClockLatency, getClockLatency)
  118. static DECLARE_FORWARD(CaptureWrapper, ALCbackend, void, lock)
  119. static DECLARE_FORWARD(CaptureWrapper, ALCbackend, void, unlock)
  120. DECLARE_DEFAULT_ALLOCATORS(CaptureWrapper)
  121. DEFINE_ALCBACKEND_VTABLE(CaptureWrapper);
  122. static void CaptureWrapper_Construct(CaptureWrapper *self, ALCdevice *device, const BackendFuncs *funcs)
  123. {
  124. ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
  125. SET_VTABLE2(CaptureWrapper, ALCbackend, self);
  126. self->Funcs = funcs;
  127. }
  128. static ALCenum CaptureWrapper_open(CaptureWrapper *self, const ALCchar *name)
  129. {
  130. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  131. return self->Funcs->OpenCapture(device, name);
  132. }
  133. static void CaptureWrapper_close(CaptureWrapper *self)
  134. {
  135. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  136. self->Funcs->CloseCapture(device);
  137. }
  138. static ALCboolean CaptureWrapper_start(CaptureWrapper *self)
  139. {
  140. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  141. self->Funcs->StartCapture(device);
  142. return ALC_TRUE;
  143. }
  144. static void CaptureWrapper_stop(CaptureWrapper *self)
  145. {
  146. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  147. self->Funcs->StopCapture(device);
  148. }
  149. static ALCenum CaptureWrapper_captureSamples(CaptureWrapper *self, void *buffer, ALCuint samples)
  150. {
  151. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  152. return self->Funcs->CaptureSamples(device, buffer, samples);
  153. }
  154. static ALCuint CaptureWrapper_availableSamples(CaptureWrapper *self)
  155. {
  156. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  157. return self->Funcs->AvailableSamples(device);
  158. }
  159. ALCbackend *create_backend_wrapper(ALCdevice *device, const BackendFuncs *funcs, ALCbackend_Type type)
  160. {
  161. if(type == ALCbackend_Playback)
  162. {
  163. PlaybackWrapper *backend;
  164. NEW_OBJ(backend, PlaybackWrapper)(device, funcs);
  165. if(!backend) return NULL;
  166. return STATIC_CAST(ALCbackend, backend);
  167. }
  168. if(type == ALCbackend_Capture)
  169. {
  170. CaptureWrapper *backend;
  171. NEW_OBJ(backend, CaptureWrapper)(device, funcs);
  172. if(!backend) return NULL;
  173. return STATIC_CAST(ALCbackend, backend);
  174. }
  175. return NULL;
  176. }