solaris.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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., 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 <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 "AL/al.h"
  33. #include "AL/alc.h"
  34. #include <sys/audioio.h>
  35. static const ALCchar solaris_device[] = "Solaris Default";
  36. static const char *solaris_driver = "/dev/audio";
  37. typedef struct {
  38. int fd;
  39. volatile int killNow;
  40. ALvoid *thread;
  41. ALubyte *mix_data;
  42. int data_size;
  43. } solaris_data;
  44. static ALuint SolarisProc(ALvoid *ptr)
  45. {
  46. ALCdevice *pDevice = (ALCdevice*)ptr;
  47. solaris_data *data = (solaris_data*)pDevice->ExtraData;
  48. ALint frameSize;
  49. int wrote;
  50. SetRTPriority();
  51. frameSize = FrameSizeFromDevFmt(pDevice->FmtChans, pDevice->FmtType);
  52. while(!data->killNow && pDevice->Connected)
  53. {
  54. ALint len = data->data_size;
  55. ALubyte *WritePtr = data->mix_data;
  56. aluMixData(pDevice, WritePtr, len/frameSize);
  57. while(len > 0 && !data->killNow)
  58. {
  59. wrote = write(data->fd, WritePtr, len);
  60. if(wrote < 0)
  61. {
  62. if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
  63. {
  64. ERR("write failed: %s\n", strerror(errno));
  65. aluHandleDisconnect(pDevice);
  66. break;
  67. }
  68. Sleep(1);
  69. continue;
  70. }
  71. len -= wrote;
  72. WritePtr += wrote;
  73. }
  74. }
  75. return 0;
  76. }
  77. static ALCenum solaris_open_playback(ALCdevice *device, const ALCchar *deviceName)
  78. {
  79. solaris_data *data;
  80. if(!deviceName)
  81. deviceName = solaris_device;
  82. else if(strcmp(deviceName, solaris_device) != 0)
  83. return ALC_INVALID_VALUE;
  84. data = (solaris_data*)calloc(1, sizeof(solaris_data));
  85. data->killNow = 0;
  86. data->fd = open(solaris_driver, O_WRONLY);
  87. if(data->fd == -1)
  88. {
  89. free(data);
  90. ERR("Could not open %s: %s\n", solaris_driver, strerror(errno));
  91. return ALC_INVALID_VALUE;
  92. }
  93. device->szDeviceName = strdup(deviceName);
  94. device->ExtraData = data;
  95. return ALC_NO_ERROR;
  96. }
  97. static void solaris_close_playback(ALCdevice *device)
  98. {
  99. solaris_data *data = (solaris_data*)device->ExtraData;
  100. close(data->fd);
  101. free(data);
  102. device->ExtraData = NULL;
  103. }
  104. static ALCboolean solaris_reset_playback(ALCdevice *device)
  105. {
  106. solaris_data *data = (solaris_data*)device->ExtraData;
  107. audio_info_t info;
  108. ALuint frameSize;
  109. int numChannels;
  110. AUDIO_INITINFO(&info);
  111. info.play.sample_rate = device->Frequency;
  112. if(device->FmtChans != DevFmtMono)
  113. device->FmtChans = DevFmtStereo;
  114. numChannels = ChannelsFromDevFmt(device->FmtChans);
  115. info.play.channels = numChannels;
  116. switch(device->FmtType)
  117. {
  118. case DevFmtByte:
  119. info.play.precision = 8;
  120. info.play.encoding = AUDIO_ENCODING_LINEAR;
  121. break;
  122. case DevFmtUByte:
  123. info.play.precision = 8;
  124. info.play.encoding = AUDIO_ENCODING_LINEAR8;
  125. break;
  126. case DevFmtUShort:
  127. case DevFmtInt:
  128. case DevFmtUInt:
  129. case DevFmtFloat:
  130. device->FmtType = DevFmtShort;
  131. /* fall-through */
  132. case DevFmtShort:
  133. info.play.precision = 16;
  134. info.play.encoding = AUDIO_ENCODING_LINEAR;
  135. break;
  136. }
  137. frameSize = numChannels * BytesFromDevFmt(device->FmtType);
  138. info.play.buffer_size = device->UpdateSize*device->NumUpdates * frameSize;
  139. if(ioctl(data->fd, AUDIO_SETINFO, &info) < 0)
  140. {
  141. ERR("ioctl failed: %s\n", strerror(errno));
  142. return ALC_FALSE;
  143. }
  144. if(ChannelsFromDevFmt(device->FmtChans) != info.play.channels)
  145. {
  146. ERR("Could not set %d channels, got %d instead\n", ChannelsFromDevFmt(device->FmtChans), info.play.channels);
  147. return ALC_FALSE;
  148. }
  149. if(!((info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR8 && device->FmtType == DevFmtUByte) ||
  150. (info.play.precision == 8 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtByte) ||
  151. (info.play.precision == 16 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtShort) ||
  152. (info.play.precision == 32 && info.play.encoding == AUDIO_ENCODING_LINEAR && device->FmtType == DevFmtInt)))
  153. {
  154. ERR("Could not set %s samples, got %d (0x%x)\n", DevFmtTypeString(device->FmtType),
  155. info.play.precision, info.play.encoding);
  156. return ALC_FALSE;
  157. }
  158. device->Frequency = info.play.sample_rate;
  159. device->UpdateSize = (info.play.buffer_size/device->NumUpdates) + 1;
  160. SetDefaultChannelOrder(device);
  161. return ALC_TRUE;
  162. }
  163. static ALCboolean solaris_start_playback(ALCdevice *device)
  164. {
  165. solaris_data *data = (solaris_data*)device->ExtraData;
  166. data->data_size = device->UpdateSize * FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
  167. data->mix_data = calloc(1, data->data_size);
  168. data->thread = StartThread(SolarisProc, device);
  169. if(data->thread == NULL)
  170. {
  171. free(data->mix_data);
  172. data->mix_data = NULL;
  173. return ALC_FALSE;
  174. }
  175. return ALC_TRUE;
  176. }
  177. static void solaris_stop_playback(ALCdevice *device)
  178. {
  179. solaris_data *data = (solaris_data*)device->ExtraData;
  180. if(!data->thread)
  181. return;
  182. data->killNow = 1;
  183. StopThread(data->thread);
  184. data->thread = NULL;
  185. data->killNow = 0;
  186. if(ioctl(data->fd, AUDIO_DRAIN) < 0)
  187. ERR("Error draining device: %s\n", strerror(errno));
  188. free(data->mix_data);
  189. data->mix_data = NULL;
  190. }
  191. static const BackendFuncs solaris_funcs = {
  192. solaris_open_playback,
  193. solaris_close_playback,
  194. solaris_reset_playback,
  195. solaris_start_playback,
  196. solaris_stop_playback,
  197. NULL,
  198. NULL,
  199. NULL,
  200. NULL,
  201. NULL,
  202. NULL
  203. };
  204. ALCboolean alc_solaris_init(BackendFuncs *func_list)
  205. {
  206. ConfigValueStr("solaris", "device", &solaris_driver);
  207. *func_list = solaris_funcs;
  208. return ALC_TRUE;
  209. }
  210. void alc_solaris_deinit(void)
  211. {
  212. }
  213. void alc_solaris_probe(enum DevProbe type)
  214. {
  215. switch(type)
  216. {
  217. case ALL_DEVICE_PROBE:
  218. {
  219. #ifdef HAVE_STAT
  220. struct stat buf;
  221. if(stat(solaris_driver, &buf) == 0)
  222. #endif
  223. AppendAllDeviceList(solaris_device);
  224. }
  225. break;
  226. case CAPTURE_DEVICE_PROBE:
  227. break;
  228. }
  229. }