base.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. int ret;
  9. self->mDevice = device;
  10. ret = almtx_init(&self->mMutex, almtx_recursive);
  11. assert(ret == althrd_success);
  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. ALint64 ALCbackend_getLatency(ALCbackend* UNUSED(self))
  30. {
  31. return 0;
  32. }
  33. void ALCbackend_lock(ALCbackend *self)
  34. {
  35. int ret = almtx_lock(&self->mMutex);
  36. assert(ret == althrd_success);
  37. }
  38. void ALCbackend_unlock(ALCbackend *self)
  39. {
  40. int ret = almtx_unlock(&self->mMutex);
  41. assert(ret == althrd_success);
  42. }
  43. /* Base ALCbackendFactory method implementations. */
  44. void ALCbackendFactory_deinit(ALCbackendFactory* UNUSED(self))
  45. {
  46. }
  47. /* Wrappers to use an old-style backend with the new interface. */
  48. typedef struct PlaybackWrapper {
  49. DERIVE_FROM_TYPE(ALCbackend);
  50. const BackendFuncs *Funcs;
  51. } PlaybackWrapper;
  52. static void PlaybackWrapper_Construct(PlaybackWrapper *self, ALCdevice *device, const BackendFuncs *funcs);
  53. static DECLARE_FORWARD(PlaybackWrapper, ALCbackend, void, Destruct)
  54. static ALCenum PlaybackWrapper_open(PlaybackWrapper *self, const ALCchar *name);
  55. static void PlaybackWrapper_close(PlaybackWrapper *self);
  56. static ALCboolean PlaybackWrapper_reset(PlaybackWrapper *self);
  57. static ALCboolean PlaybackWrapper_start(PlaybackWrapper *self);
  58. static void PlaybackWrapper_stop(PlaybackWrapper *self);
  59. static DECLARE_FORWARD2(PlaybackWrapper, ALCbackend, ALCenum, captureSamples, void*, ALCuint)
  60. static DECLARE_FORWARD(PlaybackWrapper, ALCbackend, ALCuint, availableSamples)
  61. static DECLARE_FORWARD(PlaybackWrapper, ALCbackend, ALint64, getLatency)
  62. static DECLARE_FORWARD(PlaybackWrapper, ALCbackend, void, lock)
  63. static DECLARE_FORWARD(PlaybackWrapper, ALCbackend, void, unlock)
  64. DECLARE_DEFAULT_ALLOCATORS(PlaybackWrapper)
  65. DEFINE_ALCBACKEND_VTABLE(PlaybackWrapper);
  66. static void PlaybackWrapper_Construct(PlaybackWrapper *self, ALCdevice *device, const BackendFuncs *funcs)
  67. {
  68. ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
  69. SET_VTABLE2(PlaybackWrapper, ALCbackend, self);
  70. self->Funcs = funcs;
  71. }
  72. static ALCenum PlaybackWrapper_open(PlaybackWrapper *self, const ALCchar *name)
  73. {
  74. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  75. return self->Funcs->OpenPlayback(device, name);
  76. }
  77. static void PlaybackWrapper_close(PlaybackWrapper *self)
  78. {
  79. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  80. self->Funcs->ClosePlayback(device);
  81. }
  82. static ALCboolean PlaybackWrapper_reset(PlaybackWrapper *self)
  83. {
  84. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  85. return self->Funcs->ResetPlayback(device);
  86. }
  87. static ALCboolean PlaybackWrapper_start(PlaybackWrapper *self)
  88. {
  89. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  90. return self->Funcs->StartPlayback(device);
  91. }
  92. static void PlaybackWrapper_stop(PlaybackWrapper *self)
  93. {
  94. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  95. self->Funcs->StopPlayback(device);
  96. }
  97. typedef struct CaptureWrapper {
  98. DERIVE_FROM_TYPE(ALCbackend);
  99. const BackendFuncs *Funcs;
  100. } CaptureWrapper;
  101. static void CaptureWrapper_Construct(CaptureWrapper *self, ALCdevice *device, const BackendFuncs *funcs);
  102. static DECLARE_FORWARD(CaptureWrapper, ALCbackend, void, Destruct)
  103. static ALCenum CaptureWrapper_open(CaptureWrapper *self, const ALCchar *name);
  104. static void CaptureWrapper_close(CaptureWrapper *self);
  105. static DECLARE_FORWARD(CaptureWrapper, ALCbackend, ALCboolean, reset)
  106. static ALCboolean CaptureWrapper_start(CaptureWrapper *self);
  107. static void CaptureWrapper_stop(CaptureWrapper *self);
  108. static ALCenum CaptureWrapper_captureSamples(CaptureWrapper *self, void *buffer, ALCuint samples);
  109. static ALCuint CaptureWrapper_availableSamples(CaptureWrapper *self);
  110. static DECLARE_FORWARD(CaptureWrapper, ALCbackend, ALint64, getLatency)
  111. static DECLARE_FORWARD(CaptureWrapper, ALCbackend, void, lock)
  112. static DECLARE_FORWARD(CaptureWrapper, ALCbackend, void, unlock)
  113. DECLARE_DEFAULT_ALLOCATORS(CaptureWrapper)
  114. DEFINE_ALCBACKEND_VTABLE(CaptureWrapper);
  115. static void CaptureWrapper_Construct(CaptureWrapper *self, ALCdevice *device, const BackendFuncs *funcs)
  116. {
  117. ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
  118. SET_VTABLE2(CaptureWrapper, ALCbackend, self);
  119. self->Funcs = funcs;
  120. }
  121. static ALCenum CaptureWrapper_open(CaptureWrapper *self, const ALCchar *name)
  122. {
  123. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  124. return self->Funcs->OpenCapture(device, name);
  125. }
  126. static void CaptureWrapper_close(CaptureWrapper *self)
  127. {
  128. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  129. self->Funcs->CloseCapture(device);
  130. }
  131. static ALCboolean CaptureWrapper_start(CaptureWrapper *self)
  132. {
  133. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  134. self->Funcs->StartCapture(device);
  135. return ALC_TRUE;
  136. }
  137. static void CaptureWrapper_stop(CaptureWrapper *self)
  138. {
  139. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  140. self->Funcs->StopCapture(device);
  141. }
  142. static ALCenum CaptureWrapper_captureSamples(CaptureWrapper *self, void *buffer, ALCuint samples)
  143. {
  144. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  145. return self->Funcs->CaptureSamples(device, buffer, samples);
  146. }
  147. static ALCuint CaptureWrapper_availableSamples(CaptureWrapper *self)
  148. {
  149. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  150. return self->Funcs->AvailableSamples(device);
  151. }
  152. ALCbackend *create_backend_wrapper(ALCdevice *device, const BackendFuncs *funcs, ALCbackend_Type type)
  153. {
  154. if(type == ALCbackend_Playback)
  155. {
  156. PlaybackWrapper *backend;
  157. NEW_OBJ(backend, PlaybackWrapper)(device, funcs);
  158. if(!backend) return NULL;
  159. return STATIC_CAST(ALCbackend, backend);
  160. }
  161. if(type == ALCbackend_Capture)
  162. {
  163. CaptureWrapper *backend;
  164. NEW_OBJ(backend, CaptureWrapper)(device, funcs);
  165. if(!backend) return NULL;
  166. return STATIC_CAST(ALCbackend, backend);
  167. }
  168. return NULL;
  169. }