base.c 6.6 KB

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