solaris.c 7.4 KB

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