null.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 2010 by Chris Robinson
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. * Or go to http://www.gnu.org/copyleft/lgpl.html
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #ifdef HAVE_WINDOWS_H
  23. #include <windows.h>
  24. #endif
  25. #include "alMain.h"
  26. #include "alu.h"
  27. #include "threads.h"
  28. #include "compat.h"
  29. #include "backends/base.h"
  30. typedef struct ALCnullBackend {
  31. DERIVE_FROM_TYPE(ALCbackend);
  32. ATOMIC(int) killNow;
  33. althrd_t thread;
  34. } ALCnullBackend;
  35. static int ALCnullBackend_mixerProc(void *ptr);
  36. static void ALCnullBackend_Construct(ALCnullBackend *self, ALCdevice *device);
  37. static DECLARE_FORWARD(ALCnullBackend, ALCbackend, void, Destruct)
  38. static ALCenum ALCnullBackend_open(ALCnullBackend *self, const ALCchar *name);
  39. static ALCboolean ALCnullBackend_reset(ALCnullBackend *self);
  40. static ALCboolean ALCnullBackend_start(ALCnullBackend *self);
  41. static void ALCnullBackend_stop(ALCnullBackend *self);
  42. static DECLARE_FORWARD2(ALCnullBackend, ALCbackend, ALCenum, captureSamples, void*, ALCuint)
  43. static DECLARE_FORWARD(ALCnullBackend, ALCbackend, ALCuint, availableSamples)
  44. static DECLARE_FORWARD(ALCnullBackend, ALCbackend, ClockLatency, getClockLatency)
  45. static DECLARE_FORWARD(ALCnullBackend, ALCbackend, void, lock)
  46. static DECLARE_FORWARD(ALCnullBackend, ALCbackend, void, unlock)
  47. DECLARE_DEFAULT_ALLOCATORS(ALCnullBackend)
  48. DEFINE_ALCBACKEND_VTABLE(ALCnullBackend);
  49. static const ALCchar nullDevice[] = "No Output";
  50. static void ALCnullBackend_Construct(ALCnullBackend *self, ALCdevice *device)
  51. {
  52. ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
  53. SET_VTABLE2(ALCnullBackend, ALCbackend, self);
  54. ATOMIC_INIT(&self->killNow, AL_TRUE);
  55. }
  56. static int ALCnullBackend_mixerProc(void *ptr)
  57. {
  58. ALCnullBackend *self = (ALCnullBackend*)ptr;
  59. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  60. struct timespec now, start;
  61. ALuint64 avail, done;
  62. const long restTime = (long)((ALuint64)device->UpdateSize * 1000000000 /
  63. device->Frequency / 2);
  64. SetRTPriority();
  65. althrd_setname(althrd_current(), MIXER_THREAD_NAME);
  66. done = 0;
  67. if(altimespec_get(&start, AL_TIME_UTC) != AL_TIME_UTC)
  68. {
  69. ERR("Failed to get starting time\n");
  70. return 1;
  71. }
  72. while(!ATOMIC_LOAD(&self->killNow, almemory_order_acquire) &&
  73. ATOMIC_LOAD(&device->Connected, almemory_order_acquire))
  74. {
  75. if(altimespec_get(&now, AL_TIME_UTC) != AL_TIME_UTC)
  76. {
  77. ERR("Failed to get current time\n");
  78. return 1;
  79. }
  80. avail = (now.tv_sec - start.tv_sec) * device->Frequency;
  81. avail += (ALint64)(now.tv_nsec - start.tv_nsec) * device->Frequency / 1000000000;
  82. if(avail < done)
  83. {
  84. /* Oops, time skipped backwards. Reset the number of samples done
  85. * with one update available since we (likely) just came back from
  86. * sleeping. */
  87. done = avail - device->UpdateSize;
  88. }
  89. if(avail-done < device->UpdateSize)
  90. al_nssleep(restTime);
  91. else while(avail-done >= device->UpdateSize)
  92. {
  93. ALCnullBackend_lock(self);
  94. aluMixData(device, NULL, device->UpdateSize);
  95. ALCnullBackend_unlock(self);
  96. done += device->UpdateSize;
  97. }
  98. }
  99. return 0;
  100. }
  101. static ALCenum ALCnullBackend_open(ALCnullBackend *self, const ALCchar *name)
  102. {
  103. ALCdevice *device;
  104. if(!name)
  105. name = nullDevice;
  106. else if(strcmp(name, nullDevice) != 0)
  107. return ALC_INVALID_VALUE;
  108. device = STATIC_CAST(ALCbackend, self)->mDevice;
  109. alstr_copy_cstr(&device->DeviceName, name);
  110. return ALC_NO_ERROR;
  111. }
  112. static ALCboolean ALCnullBackend_reset(ALCnullBackend *self)
  113. {
  114. SetDefaultWFXChannelOrder(STATIC_CAST(ALCbackend, self)->mDevice);
  115. return ALC_TRUE;
  116. }
  117. static ALCboolean ALCnullBackend_start(ALCnullBackend *self)
  118. {
  119. ATOMIC_STORE(&self->killNow, AL_FALSE, almemory_order_release);
  120. if(althrd_create(&self->thread, ALCnullBackend_mixerProc, self) != althrd_success)
  121. return ALC_FALSE;
  122. return ALC_TRUE;
  123. }
  124. static void ALCnullBackend_stop(ALCnullBackend *self)
  125. {
  126. int res;
  127. if(ATOMIC_EXCHANGE(&self->killNow, AL_TRUE, almemory_order_acq_rel))
  128. return;
  129. althrd_join(self->thread, &res);
  130. }
  131. typedef struct ALCnullBackendFactory {
  132. DERIVE_FROM_TYPE(ALCbackendFactory);
  133. } ALCnullBackendFactory;
  134. #define ALCNULLBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCnullBackendFactory, ALCbackendFactory) } }
  135. ALCbackendFactory *ALCnullBackendFactory_getFactory(void);
  136. static ALCboolean ALCnullBackendFactory_init(ALCnullBackendFactory *self);
  137. static DECLARE_FORWARD(ALCnullBackendFactory, ALCbackendFactory, void, deinit)
  138. static ALCboolean ALCnullBackendFactory_querySupport(ALCnullBackendFactory *self, ALCbackend_Type type);
  139. static void ALCnullBackendFactory_probe(ALCnullBackendFactory *self, enum DevProbe type);
  140. static ALCbackend* ALCnullBackendFactory_createBackend(ALCnullBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
  141. DEFINE_ALCBACKENDFACTORY_VTABLE(ALCnullBackendFactory);
  142. ALCbackendFactory *ALCnullBackendFactory_getFactory(void)
  143. {
  144. static ALCnullBackendFactory factory = ALCNULLBACKENDFACTORY_INITIALIZER;
  145. return STATIC_CAST(ALCbackendFactory, &factory);
  146. }
  147. static ALCboolean ALCnullBackendFactory_init(ALCnullBackendFactory* UNUSED(self))
  148. {
  149. return ALC_TRUE;
  150. }
  151. static ALCboolean ALCnullBackendFactory_querySupport(ALCnullBackendFactory* UNUSED(self), ALCbackend_Type type)
  152. {
  153. if(type == ALCbackend_Playback)
  154. return ALC_TRUE;
  155. return ALC_FALSE;
  156. }
  157. static void ALCnullBackendFactory_probe(ALCnullBackendFactory* UNUSED(self), enum DevProbe type)
  158. {
  159. switch(type)
  160. {
  161. case ALL_DEVICE_PROBE:
  162. AppendAllDevicesList(nullDevice);
  163. break;
  164. case CAPTURE_DEVICE_PROBE:
  165. break;
  166. }
  167. }
  168. static ALCbackend* ALCnullBackendFactory_createBackend(ALCnullBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
  169. {
  170. if(type == ALCbackend_Playback)
  171. {
  172. ALCnullBackend *backend;
  173. NEW_OBJ(backend, ALCnullBackend)(device);
  174. if(!backend) return NULL;
  175. return STATIC_CAST(ALCbackend, backend);
  176. }
  177. return NULL;
  178. }