solaris.c 11 KB

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