solaris.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. ATOMIC(ALenum) 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. ATOMIC_INIT(&self->killNow, AL_FALSE);
  69. }
  70. static void ALCsolarisBackend_Destruct(ALCsolarisBackend *self)
  71. {
  72. if(self->fd != -1)
  73. close(self->fd);
  74. self->fd = -1;
  75. free(self->mix_data);
  76. self->mix_data = NULL;
  77. self->data_size = 0;
  78. ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
  79. }
  80. static int ALCsolarisBackend_mixerProc(void *ptr)
  81. {
  82. ALCsolarisBackend *self = ptr;
  83. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  84. struct timeval timeout;
  85. ALubyte *write_ptr;
  86. ALint frame_size;
  87. ALint to_write;
  88. ssize_t wrote;
  89. fd_set wfds;
  90. int sret;
  91. SetRTPriority();
  92. althrd_setname(althrd_current(), MIXER_THREAD_NAME);
  93. frame_size = FrameSizeFromDevFmt(device->FmtChans, device->FmtType, device->AmbiOrder);
  94. ALCsolarisBackend_lock(self);
  95. while(!ATOMIC_LOAD_SEQ(&self->killNow) && device->Connected)
  96. {
  97. FD_ZERO(&wfds);
  98. FD_SET(self->fd, &wfds);
  99. timeout.tv_sec = 1;
  100. timeout.tv_usec = 0;
  101. ALCsolarisBackend_unlock(self);
  102. sret = select(self->fd+1, NULL, &wfds, NULL, &timeout);
  103. ALCsolarisBackend_lock(self);
  104. if(sret < 0)
  105. {
  106. if(errno == EINTR)
  107. continue;
  108. ERR("select failed: %s\n", strerror(errno));
  109. aluHandleDisconnect(device);
  110. break;
  111. }
  112. else if(sret == 0)
  113. {
  114. WARN("select timeout\n");
  115. continue;
  116. }
  117. write_ptr = self->mix_data;
  118. to_write = self->data_size;
  119. aluMixData(device, write_ptr, to_write/frame_size);
  120. while(to_write > 0 && !ATOMIC_LOAD_SEQ(&self->killNow))
  121. {
  122. wrote = write(self->fd, write_ptr, to_write);
  123. if(wrote < 0)
  124. {
  125. if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
  126. continue;
  127. ERR("write failed: %s\n", strerror(errno));
  128. aluHandleDisconnect(device);
  129. break;
  130. }
  131. to_write -= wrote;
  132. write_ptr += wrote;
  133. }
  134. }
  135. ALCsolarisBackend_unlock(self);
  136. return 0;
  137. }
  138. static ALCenum ALCsolarisBackend_open(ALCsolarisBackend *self, const ALCchar *name)
  139. {
  140. ALCdevice *device;
  141. if(!name)
  142. name = solaris_device;
  143. else if(strcmp(name, solaris_device) != 0)
  144. return ALC_INVALID_VALUE;
  145. self->fd = open(solaris_driver, O_WRONLY);
  146. if(self->fd == -1)
  147. {
  148. ERR("Could not open %s: %s\n", solaris_driver, strerror(errno));
  149. return ALC_INVALID_VALUE;
  150. }
  151. device = STATIC_CAST(ALCbackend,self)->mDevice;
  152. alstr_copy_cstr(&device->DeviceName, name);
  153. return ALC_NO_ERROR;
  154. }
  155. static void ALCsolarisBackend_close(ALCsolarisBackend *self)
  156. {
  157. close(self->fd);
  158. self->fd = -1;
  159. }
  160. static ALCboolean ALCsolarisBackend_reset(ALCsolarisBackend *self)
  161. {
  162. ALCdevice *device = STATIC_CAST(ALCbackend,self)->mDevice;
  163. audio_info_t info;
  164. ALsizei frameSize;
  165. ALsizei numChannels;
  166. AUDIO_INITINFO(&info);
  167. info.play.sample_rate = device->Frequency;
  168. if(device->FmtChans != DevFmtMono)
  169. device->FmtChans = DevFmtStereo;
  170. numChannels = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
  171. info.play.channels = numChannels;
  172. switch(device->FmtType)
  173. {
  174. case DevFmtByte:
  175. info.play.precision = 8;
  176. info.play.encoding = AUDIO_ENCODING_LINEAR;
  177. break;
  178. case DevFmtUByte:
  179. info.play.precision = 8;
  180. info.play.encoding = AUDIO_ENCODING_LINEAR8;
  181. break;
  182. case DevFmtUShort:
  183. case DevFmtInt:
  184. case DevFmtUInt:
  185. case DevFmtFloat:
  186. device->FmtType = DevFmtShort;
  187. /* fall-through */
  188. case DevFmtShort:
  189. info.play.precision = 16;
  190. info.play.encoding = AUDIO_ENCODING_LINEAR;
  191. break;
  192. }
  193. frameSize = numChannels * BytesFromDevFmt(device->FmtType);
  194. info.play.buffer_size = device->UpdateSize*device->NumUpdates * frameSize;
  195. if(ioctl(self->fd, AUDIO_SETINFO, &info) < 0)
  196. {
  197. ERR("ioctl failed: %s\n", strerror(errno));
  198. return ALC_FALSE;
  199. }
  200. if(ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder) != (ALsizei)info.play.channels)
  201. {
  202. ERR("Failed to set %s, got %u channels instead\n", DevFmtChannelsString(device->FmtChans), info.play.channels);
  203. return ALC_FALSE;
  204. }
  205. if(!((info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR8 && device->FmtType == DevFmtUByte) ||
  206. (info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtByte) ||
  207. (info.play.precision == 16 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtShort) ||
  208. (info.play.precision == 32 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtInt)))
  209. {
  210. ERR("Could not set %s samples, got %d (0x%x)\n", DevFmtTypeString(device->FmtType),
  211. info.play.precision, info.play.encoding);
  212. return ALC_FALSE;
  213. }
  214. device->Frequency = info.play.sample_rate;
  215. device->UpdateSize = (info.play.buffer_size/device->NumUpdates) + 1;
  216. SetDefaultChannelOrder(device);
  217. free(self->mix_data);
  218. self->data_size = device->UpdateSize * FrameSizeFromDevFmt(
  219. device->FmtChans, device->FmtType, device->AmbiOrder
  220. );
  221. self->mix_data = calloc(1, self->data_size);
  222. return ALC_TRUE;
  223. }
  224. static ALCboolean ALCsolarisBackend_start(ALCsolarisBackend *self)
  225. {
  226. ATOMIC_STORE_SEQ(&self->killNow, AL_FALSE);
  227. if(althrd_create(&self->thread, ALCsolarisBackend_mixerProc, self) != althrd_success)
  228. return ALC_FALSE;
  229. return ALC_TRUE;
  230. }
  231. static void ALCsolarisBackend_stop(ALCsolarisBackend *self)
  232. {
  233. int res;
  234. if(ATOMIC_EXCHANGE_SEQ(&self->killNow, AL_TRUE))
  235. return;
  236. althrd_join(self->thread, &res);
  237. if(ioctl(self->fd, AUDIO_DRAIN) < 0)
  238. ERR("Error draining device: %s\n", strerror(errno));
  239. }
  240. typedef struct ALCsolarisBackendFactory {
  241. DERIVE_FROM_TYPE(ALCbackendFactory);
  242. } ALCsolarisBackendFactory;
  243. #define ALCSOLARISBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCsolarisBackendFactory, ALCbackendFactory) } }
  244. ALCbackendFactory *ALCsolarisBackendFactory_getFactory(void);
  245. static ALCboolean ALCsolarisBackendFactory_init(ALCsolarisBackendFactory *self);
  246. static DECLARE_FORWARD(ALCsolarisBackendFactory, ALCbackendFactory, void, deinit)
  247. static ALCboolean ALCsolarisBackendFactory_querySupport(ALCsolarisBackendFactory *self, ALCbackend_Type type);
  248. static void ALCsolarisBackendFactory_probe(ALCsolarisBackendFactory *self, enum DevProbe type);
  249. static ALCbackend* ALCsolarisBackendFactory_createBackend(ALCsolarisBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
  250. DEFINE_ALCBACKENDFACTORY_VTABLE(ALCsolarisBackendFactory);
  251. ALCbackendFactory *ALCsolarisBackendFactory_getFactory(void)
  252. {
  253. static ALCsolarisBackendFactory factory = ALCSOLARISBACKENDFACTORY_INITIALIZER;
  254. return STATIC_CAST(ALCbackendFactory, &factory);
  255. }
  256. static ALCboolean ALCsolarisBackendFactory_init(ALCsolarisBackendFactory* UNUSED(self))
  257. {
  258. ConfigValueStr(NULL, "solaris", "device", &solaris_driver);
  259. return ALC_TRUE;
  260. }
  261. static ALCboolean ALCsolarisBackendFactory_querySupport(ALCsolarisBackendFactory* UNUSED(self), ALCbackend_Type type)
  262. {
  263. if(type == ALCbackend_Playback)
  264. return ALC_TRUE;
  265. return ALC_FALSE;
  266. }
  267. static void ALCsolarisBackendFactory_probe(ALCsolarisBackendFactory* UNUSED(self), enum DevProbe type)
  268. {
  269. switch(type)
  270. {
  271. case ALL_DEVICE_PROBE:
  272. {
  273. #ifdef HAVE_STAT
  274. struct stat buf;
  275. if(stat(solaris_driver, &buf) == 0)
  276. #endif
  277. AppendAllDevicesList(solaris_device);
  278. }
  279. break;
  280. case CAPTURE_DEVICE_PROBE:
  281. break;
  282. }
  283. }
  284. ALCbackend* ALCsolarisBackendFactory_createBackend(ALCsolarisBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
  285. {
  286. if(type == ALCbackend_Playback)
  287. {
  288. ALCsolarisBackend *backend;
  289. NEW_OBJ(backend, ALCsolarisBackend)(device);
  290. if(!backend) return NULL;
  291. return STATIC_CAST(ALCbackend, backend);
  292. }
  293. return NULL;
  294. }