null.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, 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. volatile int killNow;
  33. althread_t thread;
  34. } ALCnullBackend;
  35. DECLARE_ALCBACKEND_VTABLE(ALCnullBackend);
  36. static ALuint ALCnullBackend_mixerProc(ALvoid *ptr);
  37. static void ALCnullBackend_Construct(ALCnullBackend *self, ALCdevice *device);
  38. static DECLARE_FORWARD(ALCnullBackend, ALCbackend, void, Destruct)
  39. static ALCenum ALCnullBackend_open(ALCnullBackend *self, const ALCchar *name);
  40. static void ALCnullBackend_close(ALCnullBackend *self);
  41. static ALCboolean ALCnullBackend_reset(ALCnullBackend *self);
  42. static ALCboolean ALCnullBackend_start(ALCnullBackend *self);
  43. static void ALCnullBackend_stop(ALCnullBackend *self);
  44. static DECLARE_FORWARD2(ALCnullBackend, ALCbackend, ALCenum, captureSamples, void*, ALCuint)
  45. static DECLARE_FORWARD(ALCnullBackend, ALCbackend, ALCuint, availableSamples)
  46. static DECLARE_FORWARD(ALCnullBackend, ALCbackend, ALint64, getLatency)
  47. static DECLARE_FORWARD(ALCnullBackend, ALCbackend, void, lock)
  48. static DECLARE_FORWARD(ALCnullBackend, ALCbackend, void, unlock)
  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. }
  55. static ALuint ALCnullBackend_mixerProc(ALvoid *ptr)
  56. {
  57. ALCnullBackend *self = (ALCnullBackend*)ptr;
  58. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  59. ALuint now, start;
  60. ALuint64 avail, done;
  61. SetRTPriority();
  62. SetThreadName(MIXER_THREAD_NAME);
  63. done = 0;
  64. start = timeGetTime();
  65. while(!self->killNow && device->Connected)
  66. {
  67. now = timeGetTime();
  68. avail = (ALuint64)(now-start) * device->Frequency / 1000;
  69. if(avail < done)
  70. {
  71. /* Timer wrapped (50 days???). Add the remainder of the cycle to
  72. * the available count and reset the number of samples done */
  73. avail += (U64(1)<<32)*device->Frequency/1000 - done;
  74. done = 0;
  75. }
  76. if(avail-done < device->UpdateSize)
  77. {
  78. ALuint restTime = (ALuint)((device->UpdateSize - (avail-done)) * 1000 /
  79. device->Frequency);
  80. Sleep(restTime);
  81. continue;
  82. }
  83. do {
  84. aluMixData(device, NULL, device->UpdateSize);
  85. done += device->UpdateSize;
  86. } while(avail-done >= device->UpdateSize);
  87. }
  88. return 0;
  89. }
  90. static ALCenum ALCnullBackend_open(ALCnullBackend *self, const ALCchar *name)
  91. {
  92. ALCdevice *device;
  93. if(!name)
  94. name = nullDevice;
  95. else if(strcmp(name, nullDevice) != 0)
  96. return ALC_INVALID_VALUE;
  97. device = STATIC_CAST(ALCbackend, self)->mDevice;
  98. device->DeviceName = strdup(name);
  99. return ALC_NO_ERROR;
  100. }
  101. static void ALCnullBackend_close(ALCnullBackend* UNUSED(self))
  102. {
  103. }
  104. static ALCboolean ALCnullBackend_reset(ALCnullBackend *self)
  105. {
  106. SetDefaultWFXChannelOrder(STATIC_CAST(ALCbackend, self)->mDevice);
  107. return ALC_TRUE;
  108. }
  109. static ALCboolean ALCnullBackend_start(ALCnullBackend *self)
  110. {
  111. if(!StartThread(&self->thread, ALCnullBackend_mixerProc, self))
  112. return ALC_FALSE;
  113. return ALC_TRUE;
  114. }
  115. static void ALCnullBackend_stop(ALCnullBackend *self)
  116. {
  117. if(!self->thread)
  118. return;
  119. self->killNow = 1;
  120. StopThread(self->thread);
  121. self->thread = NULL;
  122. self->killNow = 0;
  123. }
  124. static void ALCnullBackend_Delete(ALCnullBackend *self)
  125. {
  126. free(self);
  127. }
  128. DEFINE_ALCBACKEND_VTABLE(ALCnullBackend);
  129. typedef struct ALCnullBackendFactory {
  130. DERIVE_FROM_TYPE(ALCbackendFactory);
  131. } ALCnullBackendFactory;
  132. #define ALCNULLBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCnullBackendFactory, ALCbackendFactory) } }
  133. ALCbackendFactory *ALCnullBackendFactory_getFactory(void);
  134. static ALCboolean ALCnullBackendFactory_init(ALCnullBackendFactory *self);
  135. static DECLARE_FORWARD(ALCnullBackendFactory, ALCbackendFactory, void, deinit)
  136. static ALCboolean ALCnullBackendFactory_querySupport(ALCnullBackendFactory *self, ALCbackend_Type type);
  137. static void ALCnullBackendFactory_probe(ALCnullBackendFactory *self, enum DevProbe type);
  138. static ALCbackend* ALCnullBackendFactory_createBackend(ALCnullBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
  139. DEFINE_ALCBACKENDFACTORY_VTABLE(ALCnullBackendFactory);
  140. ALCbackendFactory *ALCnullBackendFactory_getFactory(void)
  141. {
  142. static ALCnullBackendFactory factory = ALCNULLBACKENDFACTORY_INITIALIZER;
  143. return STATIC_CAST(ALCbackendFactory, &factory);
  144. }
  145. static ALCboolean ALCnullBackendFactory_init(ALCnullBackendFactory* UNUSED(self))
  146. {
  147. return ALC_TRUE;
  148. }
  149. static ALCboolean ALCnullBackendFactory_querySupport(ALCnullBackendFactory* UNUSED(self), ALCbackend_Type type)
  150. {
  151. if(type == ALCbackend_Playback)
  152. return ALC_TRUE;
  153. return ALC_FALSE;
  154. }
  155. static void ALCnullBackendFactory_probe(ALCnullBackendFactory* UNUSED(self), enum DevProbe type)
  156. {
  157. switch(type)
  158. {
  159. case ALL_DEVICE_PROBE:
  160. AppendAllDevicesList(nullDevice);
  161. break;
  162. case CAPTURE_DEVICE_PROBE:
  163. break;
  164. }
  165. }
  166. static ALCbackend* ALCnullBackendFactory_createBackend(ALCnullBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
  167. {
  168. ALCnullBackend *backend;
  169. assert(type == ALCbackend_Playback);
  170. backend = calloc(1, sizeof(*backend));
  171. if(!backend) return NULL;
  172. ALCnullBackend_Construct(backend, device);
  173. return STATIC_CAST(ALCbackend, backend);
  174. }