solaris.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2007 by authors.
  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 <sys/ioctl.h>
  22. #include <sys/types.h>
  23. #include <sys/time.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <stdlib.h>
  27. #include <stdio.h>
  28. #include <memory.h>
  29. #include <unistd.h>
  30. #include <errno.h>
  31. #include <math.h>
  32. #include "alMain.h"
  33. #include "alu.h"
  34. #include "threads.h"
  35. #include "compat.h"
  36. #include "backends/base.h"
  37. #include <sys/audioio.h>
  38. typedef struct ALCsolarisBackend {
  39. DERIVE_FROM_TYPE(ALCbackend);
  40. int fd;
  41. ALubyte *mix_data;
  42. int data_size;
  43. volatile int killNow;
  44. althrd_t thread;
  45. } ALCsolarisBackend;
  46. static int ALCsolarisBackend_mixerProc(void *ptr);
  47. static void ALCsolarisBackend_Construct(ALCsolarisBackend *self, ALCdevice *device);
  48. static void ALCsolarisBackend_Destruct(ALCsolarisBackend *self);
  49. static ALCenum ALCsolarisBackend_open(ALCsolarisBackend *self, const ALCchar *name);
  50. static void ALCsolarisBackend_close(ALCsolarisBackend *self);
  51. static ALCboolean ALCsolarisBackend_reset(ALCsolarisBackend *self);
  52. static ALCboolean ALCsolarisBackend_start(ALCsolarisBackend *self);
  53. static void ALCsolarisBackend_stop(ALCsolarisBackend *self);
  54. static DECLARE_FORWARD2(ALCsolarisBackend, ALCbackend, ALCenum, captureSamples, void*, ALCuint)
  55. static DECLARE_FORWARD(ALCsolarisBackend, ALCbackend, ALCuint, availableSamples)
  56. static DECLARE_FORWARD(ALCsolarisBackend, ALCbackend, ClockLatency, getClockLatency)
  57. static DECLARE_FORWARD(ALCsolarisBackend, ALCbackend, void, lock)
  58. static DECLARE_FORWARD(ALCsolarisBackend, ALCbackend, void, unlock)
  59. DECLARE_DEFAULT_ALLOCATORS(ALCsolarisBackend)
  60. DEFINE_ALCBACKEND_VTABLE(ALCsolarisBackend);
  61. static const ALCchar solaris_device[] = "Solaris Default";
  62. static const char *solaris_driver = "/dev/audio";
  63. static void ALCsolarisBackend_Construct(ALCsolarisBackend *self, ALCdevice *device)
  64. {
  65. ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
  66. SET_VTABLE2(ALCsolarisBackend, ALCbackend, self);
  67. self->fd = -1;
  68. }
  69. static void ALCsolarisBackend_Destruct(ALCsolarisBackend *self)
  70. {
  71. if(self->fd != -1)
  72. close(self->fd);
  73. self->fd = -1;
  74. free(self->mix_data);
  75. self->mix_data = NULL;
  76. self->data_size = 0;
  77. ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
  78. }
  79. static int ALCsolarisBackend_mixerProc(void *ptr)
  80. {
  81. ALCsolarisBackend *self = ptr;
  82. ALCdevice *Device = STATIC_CAST(ALCbackend,self)->mDevice;
  83. ALint frameSize;
  84. int wrote;
  85. SetRTPriority();
  86. althrd_setname(althrd_current(), MIXER_THREAD_NAME);
  87. frameSize = FrameSizeFromDevFmt(Device->FmtChans, Device->FmtType);
  88. while(!self->killNow && Device->Connected)
  89. {
  90. ALint len = self->data_size;
  91. ALubyte *WritePtr = self->mix_data;
  92. aluMixData(Device, WritePtr, len/frameSize);
  93. while(len > 0 && !self->killNow)
  94. {
  95. wrote = write(self->fd, WritePtr, len);
  96. if(wrote < 0)
  97. {
  98. if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
  99. {
  100. ERR("write failed: %s\n", strerror(errno));
  101. ALCsolarisBackend_lock(self);
  102. aluHandleDisconnect(Device);
  103. ALCsolarisBackend_unlock(self);
  104. break;
  105. }
  106. al_nssleep(1000000);
  107. continue;
  108. }
  109. len -= wrote;
  110. WritePtr += wrote;
  111. }
  112. }
  113. return 0;
  114. }
  115. static ALCenum ALCsolarisBackend_open(ALCsolarisBackend *self, const ALCchar *name)
  116. {
  117. ALCdevice *device;
  118. if(!name)
  119. name = solaris_device;
  120. else if(strcmp(name, solaris_device) != 0)
  121. return ALC_INVALID_VALUE;
  122. self->fd = open(solaris_driver, O_WRONLY);
  123. if(self->fd == -1)
  124. {
  125. ERR("Could not open %s: %s\n", solaris_driver, strerror(errno));
  126. return ALC_INVALID_VALUE;
  127. }
  128. device = STATIC_CAST(ALCbackend,self)->mDevice;
  129. al_string_copy_cstr(&device->DeviceName, name);
  130. return ALC_NO_ERROR;
  131. }
  132. static void ALCsolarisBackend_close(ALCsolarisBackend *self)
  133. {
  134. close(self->fd);
  135. self->fd = -1;
  136. }
  137. static ALCboolean ALCsolarisBackend_reset(ALCsolarisBackend *self)
  138. {
  139. ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
  140. audio_info_t info;
  141. ALuint frameSize;
  142. int numChannels;
  143. AUDIO_INITINFO(&info);
  144. info.play.sample_rate = device->Frequency;
  145. if(device->FmtChans != DevFmtMono)
  146. device->FmtChans = DevFmtStereo;
  147. numChannels = ChannelsFromDevFmt(device->FmtChans);
  148. info.play.channels = numChannels;
  149. switch(device->FmtType)
  150. {
  151. case DevFmtByte:
  152. info.play.precision = 8;
  153. info.play.encoding = AUDIO_ENCODING_LINEAR;
  154. break;
  155. case DevFmtUByte:
  156. info.play.precision = 8;
  157. info.play.encoding = AUDIO_ENCODING_LINEAR8;
  158. break;
  159. case DevFmtUShort:
  160. case DevFmtInt:
  161. case DevFmtUInt:
  162. case DevFmtFloat:
  163. device->FmtType = DevFmtShort;
  164. /* fall-through */
  165. case DevFmtShort:
  166. info.play.precision = 16;
  167. info.play.encoding = AUDIO_ENCODING_LINEAR;
  168. break;
  169. }
  170. frameSize = numChannels * BytesFromDevFmt(device->FmtType);
  171. info.play.buffer_size = device->UpdateSize*device->NumUpdates * frameSize;
  172. if(ioctl(self->fd, AUDIO_SETINFO, &info) < 0)
  173. {
  174. ERR("ioctl failed: %s\n", strerror(errno));
  175. return ALC_FALSE;
  176. }
  177. if(ChannelsFromDevFmt(device->FmtChans) != info.play.channels)
  178. {
  179. ERR("Could not set %d channels, got %d instead\n", ChannelsFromDevFmt(device->FmtChans), info.play.channels);
  180. return ALC_FALSE;
  181. }
  182. if(!((info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR8 && device->FmtType == DevFmtUByte) ||
  183. (info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtByte) ||
  184. (info.play.precision == 16 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtShort) ||
  185. (info.play.precision == 32 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtInt)))
  186. {
  187. ERR("Could not set %s samples, got %d (0x%x)\n", DevFmtTypeString(device->FmtType),
  188. info.play.precision, info.play.encoding);
  189. return ALC_FALSE;
  190. }
  191. device->Frequency = info.play.sample_rate;
  192. device->UpdateSize = (info.play.buffer_size/device->NumUpdates) + 1;
  193. SetDefaultChannelOrder(device);
  194. free(self->mix_data);
  195. self->data_size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
  196. self->mix_data = calloc(1, self->data_size);
  197. return ALC_TRUE;
  198. }
  199. static ALCboolean ALCsolarisBackend_start(ALCsolarisBackend *self)
  200. {
  201. self->killNow = 0;
  202. if(althrd_create(&self->thread, ALCsolarisBackend_mixerProc, self) != althrd_success)
  203. return ALC_FALSE;
  204. return ALC_TRUE;
  205. }
  206. static void ALCsolarisBackend_stop(ALCsolarisBackend *self)
  207. {
  208. int res;
  209. if(self->killNow)
  210. return;
  211. self->killNow = 1;
  212. althrd_join(self->thread, &res);
  213. if(ioctl(self->fd, AUDIO_DRAIN) < 0)
  214. ERR("Error draining device: %s\n", strerror(errno));
  215. }
  216. typedef struct ALCsolarisBackendFactory {
  217. DERIVE_FROM_TYPE(ALCbackendFactory);
  218. } ALCsolarisBackendFactory;
  219. #define ALCSOLARISBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCsolarisBackendFactory, ALCbackendFactory) } }
  220. ALCbackendFactory *ALCsolarisBackendFactory_getFactory(void);
  221. static ALCboolean ALCsolarisBackendFactory_init(ALCsolarisBackendFactory *self);
  222. static DECLARE_FORWARD(ALCsolarisBackendFactory, ALCbackendFactory, void, deinit)
  223. static ALCboolean ALCsolarisBackendFactory_querySupport(ALCsolarisBackendFactory *self, ALCbackend_Type type);
  224. static void ALCsolarisBackendFactory_probe(ALCsolarisBackendFactory *self, enum DevProbe type);
  225. static ALCbackend* ALCsolarisBackendFactory_createBackend(ALCsolarisBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
  226. DEFINE_ALCBACKENDFACTORY_VTABLE(ALCsolarisBackendFactory);
  227. ALCbackendFactory *ALCsolarisBackendFactory_getFactory(void)
  228. {
  229. static ALCsolarisBackendFactory factory = ALCSOLARISBACKENDFACTORY_INITIALIZER;
  230. return STATIC_CAST(ALCbackendFactory, &factory);
  231. }
  232. static ALCboolean ALCsolarisBackendFactory_init(ALCsolarisBackendFactory* UNUSED(self))
  233. {
  234. ConfigValueStr(NULL, "solaris", "device", &solaris_driver);
  235. return ALC_TRUE;
  236. }
  237. static ALCboolean ALCsolarisBackendFactory_querySupport(ALCsolarisBackendFactory* UNUSED(self), ALCbackend_Type type)
  238. {
  239. if(type == ALCbackend_Playback)
  240. return ALC_TRUE;
  241. return ALC_FALSE;
  242. }
  243. static void ALCsolarisBackendFactory_probe(ALCsolarisBackendFactory* UNUSED(self), enum DevProbe type)
  244. {
  245. switch(type)
  246. {
  247. case ALL_DEVICE_PROBE:
  248. {
  249. #ifdef HAVE_STAT
  250. struct stat buf;
  251. if(stat(solaris_driver, &buf) == 0)
  252. #endif
  253. AppendAllDevicesList(solaris_device);
  254. }
  255. break;
  256. case CAPTURE_DEVICE_PROBE:
  257. break;
  258. }
  259. }
  260. ALCbackend* ALCsolarisBackendFactory_createBackend(ALCsolarisBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
  261. {
  262. if(type == ALCbackend_Playback)
  263. {
  264. ALCsolarisBackend *backend;
  265. NEW_OBJ(backend, ALCsolarisBackend)(device);
  266. if(!backend) return NULL;
  267. return STATIC_CAST(ALCbackend, backend);
  268. }
  269. return NULL;
  270. }