solaris.c 10 KB

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